From 47ce62eff44f9b8204f09c5ad875c87ab7b540f3 Mon Sep 17 00:00:00 2001 From: hutiefang76 <137664623+hutiefang76@users.noreply.github.com> Date: Tue, 10 Nov 2026 21:55:03 +0800 Subject: [PATCH 1/2] [COMPRESS-643] Reset ZIP entry counts after draining previous entries Reset the uncompressed byte count after closing the previous entry so implicit draining does not leak its unread bytes into the next count. Preserve the zero count when advancing a closed or exhausted stream. Add DEFLATED data-descriptor regressions plus fully consumed, STORED, partial final entry, and closed-stream controls. Assisted-By: DeepSeek (deepseek-flash) Assisted-By: Codex --- src/changes/changes.xml | 1 + .../archivers/zip/ZipArchiveInputStream.java | 4 +- .../zip/ZipArchiveInputStreamTest.java | 114 ++++++++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index c34648d208f..ecbcee47f02 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -96,6 +96,7 @@ The type attribute can be add,update,fix,remove. [Zip] Fix Zip rcount field width in x0015 and x0016 certificate id extra fields (#787). [Zip] Reject negative ZIP64 offsets in positionAtCentralDirectory64 (#794). [Zip] UnshrinkingInputStreamnow now throws CompressorException instead of IllegalArgumetException/IllegalStateException. + [Zip] Reset ZipArchiveInputStream's uncompressed byte count after draining the previous entry. >[TAR] Uniform handling of special tar records in TarFile and TarArchiveInputStream. [TAR] TarArchiveOutputStream now throws a ArchiveException instead of an OutOfMemoryError. diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java index 59750f36a26..33a8ef37f8a 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java @@ -824,15 +824,17 @@ public ZipArchiveEntry getNextEntry() throws IOException { */ @Deprecated public ZipArchiveEntry getNextZipEntry() throws IOException { - uncompressedCount = 0; boolean firstEntry = true; if (closed || hitCentralDirectory) { + uncompressedCount = 0; return null; } if (current != null) { closeEntry(); firstEntry = false; } + // Reset the count after draining the previous entry. + uncompressedCount = 0; final long currentHeaderOffset = getBytesRead(); try { if (firstEntry) { diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java index 3bf3b7bab6d..3655bed2db0 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java @@ -45,8 +45,10 @@ import java.nio.file.Path; import java.time.Instant; import java.util.Arrays; +import java.util.zip.CRC32; import java.util.zip.ZipEntry; import java.util.zip.ZipException; +import java.util.zip.ZipOutputStream; import org.apache.commons.compress.AbstractTest; import org.apache.commons.compress.archivers.ArchiveEntry; @@ -126,6 +128,44 @@ private void extractZipInputStream(final ZipArchiveInputStream inputStream) thro } } + private static byte[] createCountArchive(final int method) throws IOException { + final byte[] first = new byte[100000]; + Arrays.fill(first, (byte) 'a'); + final byte[] second = createCountPayload(); + final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try (ZipOutputStream zos = new ZipOutputStream(bos)) { + final ZipEntry e1 = new ZipEntry("first"); + e1.setMethod(method); + if (method == ZipEntry.STORED) { + final CRC32 crc = new CRC32(); + crc.update(first); + e1.setSize(first.length); + e1.setCompressedSize(first.length); + e1.setCrc(crc.getValue()); + } + zos.putNextEntry(e1); + zos.write(first); + zos.closeEntry(); + final ZipEntry e2 = new ZipEntry("second"); + e2.setMethod(method); + if (method == ZipEntry.STORED) { + final CRC32 crc = new CRC32(); + crc.update(second); + e2.setSize(second.length); + e2.setCompressedSize(second.length); + e2.setCrc(crc.getValue()); + } + zos.putNextEntry(e2); + zos.write(second); + zos.closeEntry(); + } + return bos.toByteArray(); + } + + private static byte[] createCountPayload() { + return "COMPRESS-643 second entry payload: known ASCII text!".getBytes(StandardCharsets.US_ASCII); + } + /** * Forge a ZIP archive in memory, using STORED and Data Descriptor, and without signature of Data Descriptor. * @@ -239,6 +279,80 @@ void testGetNextZipEntry() throws IOException { } } + @ParameterizedTest + @ValueSource(ints = { 0, 1, 100000 }) + void testUncompressedCountResetAfterDeflatedPartialRead(final int consumed) throws IOException { + final byte[] archive = createCountArchive(ZipEntry.DEFLATED); + try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(archive).get()) { + final ZipArchiveEntry first = zin.getNextEntry(); + assertEquals("first", first.getName()); + assertTrue(first.getGeneralPurposeBit().usesDataDescriptor()); + final byte[] buf = new byte[consumed]; + IOUtils.readFully(zin, buf); + assertEquals(consumed, zin.getUncompressedCount()); + final ZipArchiveEntry second = zin.getNextEntry(); + assertEquals("second", second.getName()); + assertEquals(0, zin.getUncompressedCount()); + final byte[] payload = IOUtils.toByteArray(zin); + assertArrayEquals(createCountPayload(), payload); + assertEquals(payload.length, zin.getUncompressedCount()); + assertNull(zin.getNextEntry()); + assertEquals(0, zin.getUncompressedCount()); + } + } + + @ParameterizedTest + @ValueSource(ints = { 0, 1 }) + void testUncompressedCountResetAfterStoredPartialRead(final int consumed) throws IOException { + final byte[] archive = createCountArchive(ZipEntry.STORED); + try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(archive).get()) { + final ZipArchiveEntry first = zin.getNextEntry(); + assertEquals("first", first.getName()); + final byte[] buf = new byte[consumed]; + IOUtils.readFully(zin, buf); + assertEquals(consumed, zin.getUncompressedCount()); + final ZipArchiveEntry second = zin.getNextEntry(); + assertEquals("second", second.getName()); + assertEquals(0, zin.getUncompressedCount()); + final byte[] payload = IOUtils.toByteArray(zin); + assertArrayEquals(createCountPayload(), payload); + assertEquals(payload.length, zin.getUncompressedCount()); + assertNull(zin.getNextEntry()); + assertEquals(0, zin.getUncompressedCount()); + } + } + + @Test + void testUncompressedCountPartialLastEntry() throws IOException { + final byte[] archive = createCountArchive(ZipEntry.DEFLATED); + try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(archive).get()) { + assertEquals("first", zin.getNextEntry().getName()); + IOUtils.toByteArray(zin); + assertEquals("second", zin.getNextEntry().getName()); + final byte[] buf = new byte[16]; + IOUtils.readFully(zin, buf); + assertEquals(16, zin.getUncompressedCount()); + assertArrayEquals(Arrays.copyOf(createCountPayload(), 16), buf); + assertNull(zin.getNextEntry()); + assertEquals(0, zin.getUncompressedCount()); + assertNull(zin.getNextEntry()); + assertEquals(0, zin.getUncompressedCount()); + } + } + + @Test + void testUncompressedCountClosedStream() throws IOException { + final byte[] archive = createCountArchive(ZipEntry.DEFLATED); + final ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(archive).get(); + assertEquals("first", zin.getNextEntry().getName()); + final byte[] buf = new byte[5]; + IOUtils.readFully(zin, buf); + assertEquals(5, zin.getUncompressedCount()); + zin.close(); + assertNull(zin.getNextEntry()); + assertEquals(0, zin.getUncompressedCount()); + } + @Test void testGetUncompressedCountEmptyZip() throws IOException { try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(ArrayUtils.EMPTY_BYTE_ARRAY).get()) { From 06770b66cfce49e87f8e5d97013a7e12dc380261 Mon Sep 17 00:00:00 2001 From: hutiefang76 <137664623+hutiefang76@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:43:03 +0800 Subject: [PATCH 2/2] [COMPRESS-643] Leave release notes to maintainers Remove the changes.xml entry as requested in review5843396090. The ZIP reader fix and its regression tests are unchanged. Assisted-By: Codex --- src/changes/changes.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index ecbcee47f02..c34648d208f 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -96,7 +96,6 @@ The type attribute can be add,update,fix,remove. [Zip] Fix Zip rcount field width in x0015 and x0016 certificate id extra fields (#787). [Zip] Reject negative ZIP64 offsets in positionAtCentralDirectory64 (#794). [Zip] UnshrinkingInputStreamnow now throws CompressorException instead of IllegalArgumetException/IllegalStateException. - [Zip] Reset ZipArchiveInputStream's uncompressed byte count after draining the previous entry. >[TAR] Uniform handling of special tar records in TarFile and TarArchiveInputStream. [TAR] TarArchiveOutputStream now throws a ArchiveException instead of an OutOfMemoryError.