From 97b962448f38b899b5be36916efb4eb053e2838a Mon Sep 17 00:00:00 2001 From: Srikanth Reddy Lingala Date: Sun, 26 Jan 2020 19:00:27 +0100 Subject: [PATCH] Include diskNumberStart in zip64 header check and fix invalid code --- .../lingala/zip4j/headers/HeaderReader.java | 12 ++++++----- .../lingala/zip4j/headers/HeaderWriter.java | 20 ++++++++++--------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/lingala/zip4j/headers/HeaderReader.java b/src/main/java/net/lingala/zip4j/headers/HeaderReader.java index ea6bcc50..9462a030 100755 --- a/src/main/java/net/lingala/zip4j/headers/HeaderReader.java +++ b/src/main/java/net/lingala/zip4j/headers/HeaderReader.java @@ -426,7 +426,8 @@ private void readZip64ExtendedInfo(FileHeader fileHeader, RawIO rawIO) throws Zi } Zip64ExtendedInfo zip64ExtendedInfo = readZip64ExtendedInfo(fileHeader.getExtraDataRecords(), rawIO, - fileHeader.getUncompressedSize(), fileHeader.getCompressedSize(), fileHeader.getOffsetLocalHeader()); + fileHeader.getUncompressedSize(), fileHeader.getCompressedSize(), fileHeader.getOffsetLocalHeader(), + fileHeader.getDiskNumberStart()); if (zip64ExtendedInfo == null) { return; @@ -461,7 +462,7 @@ private void readZip64ExtendedInfo(LocalFileHeader localFileHeader, RawIO rawIO) } Zip64ExtendedInfo zip64ExtendedInfo = readZip64ExtendedInfo(localFileHeader.getExtraDataRecords(), rawIO, - localFileHeader.getUncompressedSize(), localFileHeader.getCompressedSize(), 0); + localFileHeader.getUncompressedSize(), localFileHeader.getCompressedSize(), 0, 0); if (zip64ExtendedInfo == null) { return; @@ -479,7 +480,8 @@ private void readZip64ExtendedInfo(LocalFileHeader localFileHeader, RawIO rawIO) } private Zip64ExtendedInfo readZip64ExtendedInfo(List extraDataRecords, RawIO rawIO, - long uncompressedSize, long compressedSize, long offsetLocalHeader) { + long uncompressedSize, long compressedSize, long offsetLocalHeader, + int diskNumberStart) { for (ExtraDataRecord extraDataRecord : extraDataRecords) { if (extraDataRecord == null) { @@ -501,7 +503,7 @@ private Zip64ExtendedInfo readZip64ExtendedInfo(List extraDataR counter += 8; } - if ( counter < extraDataRecord.getSizeOfData() && compressedSize == ZIP_64_SIZE_LIMIT) { + if (counter < extraDataRecord.getSizeOfData() && compressedSize == ZIP_64_SIZE_LIMIT) { zip64ExtendedInfo.setCompressedSize(rawIO.readLongLittleEndian(extraData, counter)); counter += 8; } @@ -511,7 +513,7 @@ private Zip64ExtendedInfo readZip64ExtendedInfo(List extraDataR counter += 8; } - if (counter < extraDataRecord.getSizeOfData() && offsetLocalHeader == ZIP_64_NUMBER_OF_ENTRIES_LIMIT) { + if (counter < extraDataRecord.getSizeOfData() && diskNumberStart == ZIP_64_NUMBER_OF_ENTRIES_LIMIT) { zip64ExtendedInfo.setDiskNumberStart(rawIO.readIntLittleEndian(extraData, counter)); } diff --git a/src/main/java/net/lingala/zip4j/headers/HeaderWriter.java b/src/main/java/net/lingala/zip4j/headers/HeaderWriter.java index 8159f907..e39a1c4e 100755 --- a/src/main/java/net/lingala/zip4j/headers/HeaderWriter.java +++ b/src/main/java/net/lingala/zip4j/headers/HeaderWriter.java @@ -37,6 +37,7 @@ import java.util.List; import static net.lingala.zip4j.util.FileUtils.getZipFileNameWithoutExtension; +import static net.lingala.zip4j.util.InternalZipConstants.ZIP_64_NUMBER_OF_ENTRIES_LIMIT; import static net.lingala.zip4j.util.InternalZipConstants.ZIP_64_SIZE_LIMIT; import static net.lingala.zip4j.util.Zip4jUtil.isStringNotNullAndNotEmpty; @@ -48,6 +49,7 @@ public class HeaderWriter { private RawIO rawIO = new RawIO(); private byte[] longBuff = new byte[8]; + private byte[] intBuff = new byte[4]; public void writeLocalFileHeader(ZipModel zipModel, LocalFileHeader localFileHeader, OutputStream outputStream, Charset charset) throws IOException { @@ -389,11 +391,7 @@ private void writeFileHeader(ZipModel zipModel, FileHeader fileHeader, ByteArray try { final byte[] emptyShortByte = {0, 0}; - boolean writeZip64ExtendedInfo = false; - - if (isZip64Entry(fileHeader)) { - writeZip64ExtendedInfo = true; - } + boolean writeZip64ExtendedInfo = isZip64Entry(fileHeader); rawIO.writeIntLittleEndian(byteArrayOutputStream, (int) fileHeader.getSignature().getValue()); rawIO.writeShortLittleEndian(byteArrayOutputStream, fileHeader.getVersionMadeBy()); @@ -446,10 +444,13 @@ private void writeFileHeader(ZipModel zipModel, FileHeader fileHeader, ByteArray } rawIO.writeShortLittleEndian(byteArrayOutputStream, fileCommentBytes.length); - //Skip disk number start for now - rawIO.writeShortLittleEndian(byteArrayOutputStream, fileHeader.getDiskNumberStart()); + if (writeZip64ExtendedInfo) { + rawIO.writeIntLittleEndian(intBuff, 0, ZIP_64_NUMBER_OF_ENTRIES_LIMIT); + byteArrayOutputStream.write(intBuff, 0, 2); + } else { + rawIO.writeShortLittleEndian(byteArrayOutputStream, fileHeader.getDiskNumberStart()); + } - //Skip internal file attributes for now byteArrayOutputStream.write(emptyShortByte); //External file attributes @@ -660,6 +661,7 @@ private long countNumberOfFileHeaderEntriesOnDisk(List fileHeaders, private boolean isZip64Entry(FileHeader fileHeader) { return fileHeader.getCompressedSize() >= ZIP_64_SIZE_LIMIT || fileHeader.getUncompressedSize() >= ZIP_64_SIZE_LIMIT - || fileHeader.getOffsetLocalHeader() >= ZIP_64_SIZE_LIMIT; + || fileHeader.getOffsetLocalHeader() >= ZIP_64_SIZE_LIMIT + || fileHeader.getDiskNumberStart() >= ZIP_64_NUMBER_OF_ENTRIES_LIMIT; } } \ No newline at end of file