From 510aef33e96dfe8d92cbced7c2d44e49cafc60a1 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 31 Oct 2023 18:04:52 +0100 Subject: [PATCH] Improved handling of io streams in end to end tests. --- .../de/jplag/endtoend/helper/FileHelper.java | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/endtoend-testing/src/main/java/de/jplag/endtoend/helper/FileHelper.java b/endtoend-testing/src/main/java/de/jplag/endtoend/helper/FileHelper.java index fa5e3e345..c4ded6e8c 100644 --- a/endtoend-testing/src/main/java/de/jplag/endtoend/helper/FileHelper.java +++ b/endtoend-testing/src/main/java/de/jplag/endtoend/helper/FileHelper.java @@ -83,24 +83,23 @@ public static void unzip(File zip, File targetDirectory) throws IOException { } else { unzippedFile.getParentFile().mkdirs(); - InputStream inputStream = zipFile.getInputStream(entry); - OutputStream outputStream = new FileOutputStream(unzippedFile); - byte[] buffer = new byte[2048]; - int count; - while ((count = inputStream.read(buffer)) > 0) { - outputStream.write(buffer, 0, count); - - totalSizeArchive += count; - totalSizeEntry += count; - - double compressionRate = (double) totalSizeEntry / entry.getCompressedSize(); - if (compressionRate > ZIP_THRESHOLD_RATIO) { - throw new IllegalStateException(String.format(ZIP_BOMB_ERROR_MESSAGE, zip.getAbsolutePath())); + try (InputStream inputStream = zipFile.getInputStream(entry)) { + try (OutputStream outputStream = new FileOutputStream(unzippedFile)) { + byte[] buffer = new byte[2048]; + int count; + while ((count = inputStream.read(buffer)) > 0) { + outputStream.write(buffer, 0, count); + + totalSizeArchive += count; + totalSizeEntry += count; + + double compressionRate = (double) totalSizeEntry / entry.getCompressedSize(); + if (compressionRate > ZIP_THRESHOLD_RATIO) { + throw new IllegalStateException(String.format(ZIP_BOMB_ERROR_MESSAGE, zip.getAbsolutePath())); + } + } } } - - inputStream.close(); - outputStream.close(); } }