From dc5754065928d6dc61f4beb141149cfb0c53c889 Mon Sep 17 00:00:00 2001 From: Chris Earle Date: Mon, 11 Nov 2024 16:45:07 -0700 Subject: [PATCH] [Archive] Only add File Entries to Zip Stream 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. --- src/main/java/co/elastic/support/util/ArchiveUtils.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/co/elastic/support/util/ArchiveUtils.java b/src/main/java/co/elastic/support/util/ArchiveUtils.java index 9170210b..ae224191 100644 --- a/src/main/java/co/elastic/support/util/ArchiveUtils.java +++ b/src/main/java/co/elastic/support/util/ArchiveUtils.java @@ -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); } @@ -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); }