Skip to content

Commit

Permalink
[Archive] Only add File Entries to Zip Stream
Browse files Browse the repository at this point in the history
This ensures that there is nothing added, but not closed if there
are any operating system "files" that are not actually file or
directory types.
  • Loading branch information
pickypg committed Nov 11, 2024
1 parent cb41c72 commit dc57540
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/main/java/co/elastic/support/util/ArchiveUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ public class ArchiveUtils {
public static File createZipArchive(String dir, String archiveFileName) throws DiagnosticException {
File srcDir = new File(dir);
String filename = dir + "-" + archiveFileName + ".zip";
File file = new File(filename);

try (
FileOutputStream fout = new FileOutputStream(filename);
ZipArchiveOutputStream taos = new ZipArchiveOutputStream(fout)) {
archiveResultsZip(archiveFileName, taos, srcDir, null, true);
logger.info(Constants.CONSOLE, "Archive: " + filename + " was created");
return file;
return new File(filename);
} catch (IOException ioe) {
throw new DiagnosticException("Couldn't create zip archive.", ioe);
}
Expand All @@ -51,15 +50,15 @@ private static void archiveResultsZip(
relPath += "-" + archiveFilename;
}

zipFileStream.putArchiveEntry(new ZipArchiveEntry(file, relPath));

if (file.isFile()) {
zipFileStream.putArchiveEntry(new ZipArchiveEntry(file, relPath));

try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
IOUtils.copy(bis, zipFileStream);
} finally {
zipFileStream.closeArchiveEntry();
}
} else if (file.isDirectory()) {
zipFileStream.closeArchiveEntry();
for (File childFile : file.listFiles()) {
archiveResultsZip(archiveFilename, zipFileStream, childFile, relPath, false);
}
Expand Down

0 comments on commit dc57540

Please sign in to comment.