This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push: new 237879c39 [COMPRESS-696] ZipArchiveInputStream.getCompressedCount() throws NullPointerException if called before getNextEntry() new 1346ce400 Merge branch 'master' of https://gitbox.apache.org/repos/asf/commons-compress.git 237879c39 is described below commit 237879c39c30d54e4899827260addafc66ccc3a4 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Tue Mar 25 09:59:42 2025 -0400 [COMPRESS-696] ZipArchiveInputStream.getCompressedCount() throws NullPointerException if called before getNextEntry() --- src/changes/changes.xml | 1 + .../compress/archivers/zip/ZipArchiveInputStream.java | 3 +++ .../compress/archivers/zip/ZipArchiveInputStreamTest.java | 15 +++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9552e0d44..2cb45c529 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -70,6 +70,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate ArjArchiveEntry.HostOs.HostOs().</action> <action type="fix" dev="sebb">Drop coveralls reference (no longer needed)</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Some ZIP operations won't read all data from a non-blocking file channel.</action> + <action type="fix" dev="ggregory" due-to="Steve Roughley, Gary Gregory" issue="COMPRESS-696">ZipArchiveInputStream.getCompressedCount() throws NullPointerException if called before getNextEntry().</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.getModificationInstant().</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.setModificationInstant(Instant).</action> 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 c26220b0d..4feaeffef 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 @@ -626,6 +626,9 @@ private long getBytesInflated() { @SuppressWarnings("resource") // checkInputStream() does not allocate. @Override public long getCompressedCount() { + if (current == null) { + return -1; + } final int method = current.entry.getMethod(); if (method == ZipArchiveOutputStream.STORED) { return current.bytesRead; 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 c4a24a066..c19ee5369 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 @@ -180,6 +180,7 @@ private void multiByteReadConsistentlyReturnsMinusOneAtEof(final File file) thro final byte[] buf = new byte[2]; try (InputStream in = newInputStream("bla.zip"); ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) { + assertEquals(-1, archive.getCompressedCount()); assertNotNull(archive.getNextEntry()); IOUtils.toByteArray(archive); assertEquals(-1, archive.read(buf)); @@ -197,6 +198,13 @@ private void singleByteReadConsistentlyReturnsMinusOneAtEof(final File file) thr } } + @Test + public void testGetCompressedCountEmptyZip() throws IOException { + try (ZipArchiveInputStream zin = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) { + assertEquals(-1, zin.getCompressedCount()); + } + } + @Test public void testGetFirstEntryEmptyZip() throws IOException { try (ZipArchiveInputStream zin = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) { @@ -205,6 +213,13 @@ public void testGetFirstEntryEmptyZip() throws IOException { } } + @Test + public void testGetUncompressedCountEmptyZip() throws IOException { + try (ZipArchiveInputStream zin = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) { + assertEquals(0, zin.getUncompressedCount()); + } + } + /** * Test case for <a href="https://issues.apache.org/jira/browse/COMPRESS-351">COMPRESS-351</a>. */