Skip to content

Commit dc57540

Browse files
committed
[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.
1 parent cb41c72 commit dc57540

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/main/java/co/elastic/support/util/ArchiveUtils.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ public class ArchiveUtils {
2525
public static File createZipArchive(String dir, String archiveFileName) throws DiagnosticException {
2626
File srcDir = new File(dir);
2727
String filename = dir + "-" + archiveFileName + ".zip";
28-
File file = new File(filename);
2928

3029
try (
3130
FileOutputStream fout = new FileOutputStream(filename);
3231
ZipArchiveOutputStream taos = new ZipArchiveOutputStream(fout)) {
3332
archiveResultsZip(archiveFileName, taos, srcDir, null, true);
3433
logger.info(Constants.CONSOLE, "Archive: " + filename + " was created");
35-
return file;
34+
return new File(filename);
3635
} catch (IOException ioe) {
3736
throw new DiagnosticException("Couldn't create zip archive.", ioe);
3837
}
@@ -51,15 +50,15 @@ private static void archiveResultsZip(
5150
relPath += "-" + archiveFilename;
5251
}
5352

54-
zipFileStream.putArchiveEntry(new ZipArchiveEntry(file, relPath));
55-
5653
if (file.isFile()) {
54+
zipFileStream.putArchiveEntry(new ZipArchiveEntry(file, relPath));
55+
5756
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
5857
IOUtils.copy(bis, zipFileStream);
58+
} finally {
5959
zipFileStream.closeArchiveEntry();
6060
}
6161
} else if (file.isDirectory()) {
62-
zipFileStream.closeArchiveEntry();
6362
for (File childFile : file.listFiles()) {
6463
archiveResultsZip(archiveFilename, zipFileStream, childFile, relPath, false);
6564
}

0 commit comments

Comments
 (0)