Repository: commons-compress
Updated Branches:
  refs/heads/master 6e1702e95 -> 115230997


COMPRESS-466 extract logic that calculates data offset in ZipFile


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

Branch: refs/heads/master
Commit: 1152309971ae0e31ced5365e81e5c80af8bf251c
Parents: 6e1702e
Author: Stefan Bodewig <bode...@apache.org>
Authored: Sun Oct 7 17:06:57 2018 +0200
Committer: Stefan Bodewig <bode...@apache.org>
Committed: Sun Oct 7 17:06:57 2018 +0200

----------------------------------------------------------------------
 .../commons/compress/archivers/zip/ZipFile.java | 44 ++++++++++++++------
 1 file changed, 32 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/11523099/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java 
b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
index 6beedcb..d72300d 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
@@ -42,6 +42,7 @@ import java.util.Map;
 import java.util.zip.Inflater;
 import java.util.zip.ZipException;
 
+import org.apache.commons.compress.archivers.EntryStreamOffsets;
 import 
org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
 import 
org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream;
 import org.apache.commons.compress.utils.CountingInputStream;
@@ -441,6 +442,9 @@ public class ZipFile implements Closeable {
             return null;
         }
         final long start = ze.getDataOffset();
+        if (start == EntryStreamOffsets.OFFSET_UNKNOWN) {
+            return null;
+        }
         return createBoundedInputStream(start, ze.getCompressedSize());
     }
 
@@ -480,7 +484,7 @@ public class ZipFile implements Closeable {
         }
         // cast validity is checked just above
         ZipUtil.checkRequestedFeatures(ze);
-        final long start = ze.getDataOffset();
+        final long start = getDataOffset(ze);
 
         // doesn't get closed if the method is not supported - which
         // should never happen because of the checkRequestedFeatures
@@ -1045,21 +1049,13 @@ public class ZipFile implements Closeable {
             // entries is filled in populateFromCentralDirectory and
             // never modified
             final Entry ze = (Entry) zipArchiveEntry;
-            final long offset = ze.getLocalHeaderOffset();
-            archive.position(offset + LFH_OFFSET_FOR_FILENAME_LENGTH);
-            wordBbuf.rewind();
-            IOUtils.readFully(archive, wordBbuf);
-            wordBbuf.flip();
-            wordBbuf.get(shortBuf);
-            final int fileNameLen = ZipShort.getValue(shortBuf);
-            wordBbuf.get(shortBuf);
-            final int extraFieldLen = ZipShort.getValue(shortBuf);
+            int[] lens = setDataOffset(ze);
+            final int fileNameLen = lens[0];
+            final int extraFieldLen = lens[1];
             skipBytes(fileNameLen);
             final byte[] localExtraData = new byte[extraFieldLen];
             IOUtils.readFully(archive, ByteBuffer.wrap(localExtraData));
             ze.setExtra(localExtraData);
-            ze.setDataOffset(offset + LFH_OFFSET_FOR_FILENAME_LENGTH
-                + SHORT + SHORT + fileNameLen + extraFieldLen);
             ze.setStreamContiguous(true);
 
             if (entriesWithoutUTF8Flag.containsKey(ze)) {
@@ -1078,6 +1074,30 @@ public class ZipFile implements Closeable {
         }
     }
 
+    private int[] setDataOffset(ZipArchiveEntry ze) throws IOException {
+        final long offset = ze.getLocalHeaderOffset();
+        archive.position(offset + LFH_OFFSET_FOR_FILENAME_LENGTH);
+        wordBbuf.rewind();
+        IOUtils.readFully(archive, wordBbuf);
+        wordBbuf.flip();
+        wordBbuf.get(shortBuf);
+        final int fileNameLen = ZipShort.getValue(shortBuf);
+        wordBbuf.get(shortBuf);
+        final int extraFieldLen = ZipShort.getValue(shortBuf);
+        ze.setDataOffset(offset + LFH_OFFSET_FOR_FILENAME_LENGTH
+                         + SHORT + SHORT + fileNameLen + extraFieldLen);
+        return new int[] { fileNameLen, extraFieldLen };
+    }
+
+    private long getDataOffset(ZipArchiveEntry ze) throws IOException {
+        long s = ze.getDataOffset();
+        if (s == EntryStreamOffsets.OFFSET_UNKNOWN) {
+            setDataOffset(ze);
+            return ze.getDataOffset();
+        }
+        return s;
+    }
+
     /**
      * Checks whether the archive starts with a LFH.  If it doesn't,
      * it may be an empty archive.

Reply via email to