Skip to content

Commit

Permalink
Improved handling of io streams in end to end tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoOfTwelve committed Oct 31, 2023
1 parent 8d26824 commit 510aef3
Showing 1 changed file with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 510aef3

Please sign in to comment.