You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2016/10/07 16:20:56 UTC

[1/2] commons-compress git commit: ZipArchiveInputStream.closeEntry fails to advance

Repository: commons-compress
Updated Branches:
  refs/heads/master f0102601d -> 9e32a2ce7


ZipArchiveInputStream.closeEntry fails to advance

Properly advances to next entry upon calling closeEntry when there
are extra junk bytes after the compressed data section of a
zip entry. This applies to the ZipArchiveInputStream implementation.


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/658812c8
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/658812c8
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/658812c8

Branch: refs/heads/master
Commit: 658812c85fe762e361c998161cfb61bd477e1b24
Parents: f010260
Author: Mike Mole <mi...@lookout.com>
Authored: Fri Aug 19 09:08:42 2016 -0400
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Fri Oct 7 18:18:13 2016 +0200

----------------------------------------------------------------------
 .../archivers/zip/ZipArchiveInputStream.java    |  21 ++++++++++--
 .../zip/ZipArchiveInputStreamTest.java          |  34 +++++++++++++++++++
 .../resources/archive_with_bytes_after_data.zip | Bin 0 -> 670 bytes
 3 files changed, 53 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/658812c8/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
----------------------------------------------------------------------
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 54aa0ab..5d4c0a8 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
@@ -616,8 +616,7 @@ public class ZipArchiveInputStream extends ArchiveInputStream {
         }
 
         // Ensure all entry bytes are read
-        if (current.bytesReadFromStream <= current.entry.getCompressedSize()
-                && !current.hasDataDescriptor) {
+        if (currentEntryHasOutstandingBytes()) {
             drainCurrentEntryData();
         } else {
             skip(Long.MAX_VALUE);
@@ -632,6 +631,12 @@ public class ZipArchiveInputStream extends ArchiveInputStream {
             // Pushback any required bytes
             if (diff > 0) {
                 pushback(buf.array(), buf.limit() - diff, diff);
+                current.bytesReadFromStream -= diff;
+            }
+
+            // Drain remainder of entry if not all data bytes were required
+            if (currentEntryHasOutstandingBytes()) {
+                drainCurrentEntryData();
             }
         }
 
@@ -646,6 +651,18 @@ public class ZipArchiveInputStream extends ArchiveInputStream {
     }
 
     /**
+     * If the compressed size of the current entry is included in the entry header
+     * and there are any outstanding bytes in the underlying stream, then
+     * this returns true.
+     *
+     * @return true, if current entry is determined to have outstanding bytes, false otherwise
+     */
+    private boolean currentEntryHasOutstandingBytes() {
+        return current.bytesReadFromStream <= current.entry.getCompressedSize()
+                && !current.hasDataDescriptor;
+    }
+
+    /**
      * Read all data of the current entry from the underlying stream
      * that hasn't been read, yet.
      */

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/658812c8/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
----------------------------------------------------------------------
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 931387c..297de9a 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
@@ -200,4 +200,38 @@ public class ZipArchiveInputStreamTest {
             assertArrayEquals(expected, IOUtils.toByteArray(in));
         }
     }
+
+    /**
+     * Test case for
+     * <a href="https://issues.apache.org/jira/browse/COMPRESS-364"
+     * >COMPRESS-364</a>.
+     */
+    @Test
+    public void testWithBytesAfterData() throws Exception {
+        final int expectedNumEntries = 2;
+        final InputStream is = ZipArchiveInputStreamTest.class
+                .getResourceAsStream("/archive_with_bytes_after_data.zip");
+        final ZipArchiveInputStream zip = new ZipArchiveInputStream(is);
+
+        try {
+            int actualNumEntries = 0;
+            ZipArchiveEntry zae = zip.getNextZipEntry();
+            while (zae != null) {
+                actualNumEntries++;
+                readEntry(zip, zae);
+                zae = zip.getNextZipEntry();
+            }
+            assertEquals(expectedNumEntries, actualNumEntries);
+        } finally {
+            zip.close();
+        }
+    }
+
+    private static byte[] readEntry(ZipArchiveInputStream zip, ZipArchiveEntry zae) throws IOException {
+        final int len = (int)zae.getSize();
+        final byte[] buff = new byte[len];
+        zip.read(buff, 0, len);
+
+        return buff;
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/658812c8/src/test/resources/archive_with_bytes_after_data.zip
----------------------------------------------------------------------
diff --git a/src/test/resources/archive_with_bytes_after_data.zip b/src/test/resources/archive_with_bytes_after_data.zip
new file mode 100644
index 0000000..8937ede
Binary files /dev/null and b/src/test/resources/archive_with_bytes_after_data.zip differ


[2/2] commons-compress git commit: record fix for COMPRESS-364

Posted by bo...@apache.org.
record fix for COMPRESS-364


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/9e32a2ce
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/9e32a2ce
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/9e32a2ce

Branch: refs/heads/master
Commit: 9e32a2ce77b9cc28e1e34dd5efeb9de48f5bc006
Parents: 658812c
Author: Stefan Bodewig <bo...@apache.org>
Authored: Fri Oct 7 18:20:28 2016 +0200
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Fri Oct 7 18:20:28 2016 +0200

----------------------------------------------------------------------
 src/changes/changes.xml | 5 +++++
 1 file changed, 5 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/9e32a2ce/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7fe883a..1bf494a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -56,6 +56,11 @@ The <action> type attribute can be add,update,fix,remove.
         Clarified which TarArchiveEntry methods are useless for
         entries read from an archive.
       </action>
+      <action issue="COMPRESS-364" type="fix" date="2016-10-07"
+              due-to="Mike Mole">
+        ZipArchiveInputStream.closeEntry does not properly advance to
+        next entry if there are junk bytes at end of data section
+      </action>
     </release>
     <release version="1.12" date="2016-06-21"
              description="Release 1.12 - API compatible to 1.11 but requires Java 6 at runtime.