Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
1.21, 1.23.0
-
None
-
None
Description
Context
During iterating over the zip entries by using ZipArchiveInputStream the provided
getCompressedCount and getUncompressedCount methods do not return the correct values when the stream content is not fully read.Demo
The zip file used in the code snippets attached to the jira.
Good behaviour
Executing the follow code working as expected:
final ZipArchiveInputStream stream = new ZipArchiveInputStream(new FileInputStream("test.zip")); while (true) { final ZipArchiveEntry nextZipEntry = stream.getNextZipEntry(); if (null == nextZipEntry) { break; } //reading all the content stream.readAllBytes(); System.out.println(String.format("[%s] compressed size: [%d] uncompressed size: [%d], calculated ratio: [%.2f]", nextZipEntry.getName(), stream.getCompressedCount(), stream.getUncompressedCount(), (double) stream.getCompressedCount() / stream.getUncompressedCount())); }
Output:
[first.xml] compressed size: [475830] uncompressed size: [16239665], calculated ratio: [0.03] [last.xml] compressed size: [2221] uncompressed size: [45481], calculated ratio: [0.05]
Bad behaviour
The next code snippet doesn't read the second entry fully only 16 bytes, and in this case the calculated values are wrong.
final ZipArchiveInputStream stream = new ZipArchiveInputStream(new FileInputStream("test.zip")); while (true) { final ZipArchiveEntry nextZipEntry = stream.getNextZipEntry(); if (null == nextZipEntry) { break; } //reading fully only the last entry if ("first.xml".equals(nextZipEntry.getName())) { stream.readAllBytes(); } else { stream.readNBytes(16); } System.out.println(String.format("[%s] compressed size: [%d] uncompressed size: [%d], calculated ratio: [%.2f]", nextZipEntry.getName(), stream.getCompressedCount(), stream.getUncompressedCount(), (double) stream.getCompressedCount() / stream.getUncompressedCount())); }
Output:
[first.xml] compressed size: [475830] uncompressed size: [16239665], calculated ratio: [0.03] [last.xml] compressed size: [81] uncompressed size: [16], calculated ratio: [5.06]
The calculated ratio is wrong for the last.xml due to the provided compressed size and uncompressed size.
This issue is also reproducible in case of iterating over the zip entries and read the content only for the last entry.