You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/02/23 16:13:36 UTC

(commons-compress) branch master updated: COMPRESS-664 - return null value from getNextEntry() for empty file (#486)

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 fe4dfb0d3 COMPRESS-664 - return null value from getNextEntry() for empty file (#486)
fe4dfb0d3 is described below

commit fe4dfb0d32fecc3c045c39369e52cc2a6b8eb16f
Author: Radek Kraus <63...@users.noreply.github.com>
AuthorDate: Fri Feb 23 17:13:30 2024 +0100

    COMPRESS-664 - return null value from getNextEntry() for empty file (#486)
    
    Fix regression error in case when getNextEntry() method is invoked on
    empty file (zero size) to get first entry. Till version 1.25.0, null
    value was returned (like is done in standard java ZipInputStream too).
    The regression was caused by implementing 'Support preamble garbage
    in ZipArchiveInputStream' feature (PR 471).
     - https://github.com/apache/commons-compress/pull/471
---
 .../commons/compress/archivers/zip/ZipArchiveInputStream.java    | 6 +++---
 .../compress/archivers/zip/ZipArchiveInputStreamTest.java        | 9 +++++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

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 3171c0182..92165df7d 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
@@ -1012,10 +1012,10 @@ public class ZipArchiveInputStream extends ArchiveInputStream<ZipArchiveEntry> i
      * Fills the given array with the first local file header and deals with splitting/spanning markers that may prefix the first LFH.
      */
     private boolean readFirstLocalFileHeader() throws IOException {
+        // for empty archive, we may get only EOCD size:
+        final byte[] header = new byte[Math.min(LFH_LEN, ZipFile.MIN_EOCD_SIZE)];
+        readFully(header);
         try {
-            // for empty archive, we may get only EOCD size:
-            final byte[] header = new byte[Math.min(LFH_LEN, ZipFile.MIN_EOCD_SIZE)];
-            readFully(header);
             READ_LOOP: for (int i = 0; ; ) {
                 for (int j = 0; i <= PREAMBLE_GARBAGE_MAX_SIZE - 4 && j <= header.length - 4; ++j, ++i) {
                     final ZipLong sig = new ZipLong(header, j);
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 4bd0c5be9..b412af6e1 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
@@ -772,4 +772,13 @@ public class ZipArchiveInputStreamTest extends AbstractTest {
             // Ignore expected exception
         }
     }
+
+    @Test
+    public void testGetFirstEntryEmptyZip() throws IOException {
+        try (ZipArchiveInputStream zin = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) {
+            ZipArchiveEntry entry = zin.getNextEntry();
+            assertNull(entry);
+        }
+    }
+
 }