Skip to content

Commit d601ec2

Browse files
committed
Fix initial jvmBaseFileOffset value for scanning for LOC entries being 0 when it should be > 0 in some cases
1 parent a31ea49 commit d601ec2

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/main/java/software/coley/lljzip/format/read/JvmZipReader.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public JvmZipReader() {
3737
/**
3838
* New reader with given allocator.
3939
*
40-
* @param allocator Allocator to use.
40+
* @param allocator
41+
* Allocator to use.
4142
*/
4243
public JvmZipReader(@Nonnull ZipPartAllocator allocator) {
4344
super(allocator);
@@ -107,7 +108,13 @@ public void read(@Nonnull ZipArchive zip, @Nonnull ByteData data) throws IOExcep
107108
// or if the prior ZIP detection was bogus.
108109
if (priorZipEndWasBogus || precedingEndOfCentralDirectory == -1L) {
109110
// There was no match for a prior end part. We will seek forwards until finding a *VALID* PK starting header.
110-
jvmBaseFileOffset = ByteDataUtil.indexOfWord(data, 0, ZipPatterns.PK_WORD);
111+
// - Java's zip parser does not always start from zero. It uses the computation:
112+
// locpos = (end.endpos - end.cenlen) - end.cenoff;
113+
// - This computation is taken from: ZipFile.Source#initCEN
114+
jvmBaseFileOffset = (end.offset() - end.getCentralDirectorySize()) - end.getCentralDirectoryOffset();
115+
116+
// Now that we have the start offset, scan forward. We can match the current value as well.
117+
jvmBaseFileOffset = ByteDataUtil.indexOfWord(data, jvmBaseFileOffset, ZipPatterns.PK_WORD);
111118
while (jvmBaseFileOffset >= 0L) {
112119
// Check that the PK discovered represents a valid zip part
113120
try {

0 commit comments

Comments
 (0)