[Git][java-team/zip4j][upstream] New upstream version 2.2.8

Andrius Merkys gitlab at salsa.debian.org
Mon Dec 30 13:51:43 GMT 2019



Andrius Merkys pushed to branch upstream at Debian Java Maintainers / zip4j


Commits:
6f0d423b by Andrius Merkys at 2019-12-30T13:19:29Z
New upstream version 2.2.8
- - - - -


12 changed files:

- README.md
- pom.xml
- src/main/java/net/lingala/zip4j/ZipFile.java
- src/main/java/net/lingala/zip4j/headers/HeaderReader.java
- src/main/java/net/lingala/zip4j/headers/HeaderWriter.java
- src/main/java/net/lingala/zip4j/io/inputstream/CipherInputStream.java
- src/main/java/net/lingala/zip4j/io/inputstream/InflaterInputStream.java
- src/main/java/net/lingala/zip4j/io/inputstream/ZipInputStream.java
- src/test/java/net/lingala/zip4j/ExtractZipFileIT.java
- src/test/java/net/lingala/zip4j/MiscZipFileIT.java
- src/test/java/net/lingala/zip4j/headers/HeaderReaderIT.java
- + src/test/resources/test-archives/invalid_zip_file_size_less_than_22kb.zip


Changes:

=====================================
README.md
=====================================
@@ -61,7 +61,7 @@ once again, and makes me support Zip4j as much as I can.
 <dependency>
     <groupId>net.lingala.zip4j</groupId>
     <artifactId>zip4j</artifactId>
-    <version>2.2.6</version>
+    <version>2.2.8</version>
 </dependency>
 ~~~~
 


=====================================
pom.xml
=====================================
@@ -6,7 +6,7 @@
 
     <groupId>net.lingala.zip4j</groupId>
     <artifactId>zip4j</artifactId>
-    <version>2.2.7-SNAPSHOT</version>
+    <version>2.2.9-SNAPSHOT</version>
 
     <name>Zip4j</name>
     <description>Zip4j - A Java library for zip files and streams</description>


=====================================
src/main/java/net/lingala/zip4j/ZipFile.java
=====================================
@@ -280,7 +280,7 @@ public class ZipFile {
 
     assertFilesExist(filesToAdd);
 
-    checkZipModel();
+    readZipInfo();
 
     if (zipModel == null) {
       throw new ZipException("internal error: zip model is null");
@@ -350,7 +350,7 @@ public class ZipFile {
    */
   private void addFolder(File folderToAdd, ZipParameters zipParameters, boolean checkSplitArchive) throws ZipException {
 
-    checkZipModel();
+    readZipInfo();
 
     if (zipModel == null) {
       throw new ZipException("internal error: zip model is null");
@@ -389,7 +389,7 @@ public class ZipFile {
 
     this.setRunInThread(false);
 
-    checkZipModel();
+    readZipInfo();
 
     if (zipModel == null) {
       throw new ZipException("internal error: zip model is null");
@@ -696,7 +696,7 @@ public class ZipFile {
       throw new ZipException("output Zip File already exists");
     }
 
-    checkZipModel();
+    readZipInfo();
 
     if (this.zipModel == null) {
       throw new ZipException("zip model is null, corrupt zip file?");
@@ -746,7 +746,7 @@ public class ZipFile {
       throw new ZipException("zip file does not exist, cannot read comment");
     }
 
-    checkZipModel();
+    readZipInfo();
 
     if (zipModel == null) {
       throw new ZipException("zip model is null, cannot read comment");
@@ -773,7 +773,7 @@ public class ZipFile {
       throw new ZipException("FileHeader is null, cannot get InputStream");
     }
 
-    checkZipModel();
+    readZipInfo();
 
     if (zipModel == null) {
       throw new ZipException("zip model is null, cannot get inputstream");
@@ -813,7 +813,7 @@ public class ZipFile {
    * @throws ZipException
    */
   public List<File> getSplitZipFiles() throws ZipException {
-    checkZipModel();
+    readZipInfo();
     return FileUtils.getSplitZipFiles(zipModel);
   }
 
@@ -827,12 +827,16 @@ public class ZipFile {
 
   /**
    * Reads the zip header information for this zip file. If the zip file
-   * does not exist, then this method throws an exception.<br><br>
+   * does not exist, it creates an empty zip model.<br><br>
    * <b>Note:</b> This method does not read local file header information
    *
    * @throws ZipException
    */
   private void readZipInfo() throws ZipException {
+    if (zipModel != null) {
+      return;
+    }
+
     if (!zipFile.exists()) {
       createNewZipModel();
       return;
@@ -853,17 +857,6 @@ public class ZipFile {
     }
   }
 
-  /**
-   * Loads the zip model if zip model is null and if zip file exists.
-   *
-   * @throws ZipException
-   */
-  private void checkZipModel() throws ZipException {
-    if (zipModel == null) {
-      readZipInfo();
-    }
-  }
-
   /**
    * Creates a new instance of zip model
    *


=====================================
src/main/java/net/lingala/zip4j/headers/HeaderReader.java
=====================================
@@ -33,7 +33,6 @@ import net.lingala.zip4j.model.enums.AesKeyStrength;
 import net.lingala.zip4j.model.enums.AesVersion;
 import net.lingala.zip4j.model.enums.CompressionMethod;
 import net.lingala.zip4j.model.enums.EncryptionMethod;
-import net.lingala.zip4j.util.InternalZipConstants;
 import net.lingala.zip4j.util.RawIO;
 
 import java.io.IOException;
@@ -61,10 +60,16 @@ public class HeaderReader {
   private byte[] intBuff = new byte[4];
 
   public ZipModel readAllHeaders(RandomAccessFile zip4jRaf, Charset charset) throws IOException {
+
+    if (zip4jRaf.length() < ENDHDR) {
+      throw new ZipException("Zip file size less than minimum expected zip file size. " +
+          "Probably not a zip file or a corrupted zip file");
+    }
+
     zipModel = new ZipModel();
 
     try {
-      zipModel.setEndOfCentralDirectoryRecord(readEndOfCentralDirectoryRecord(zip4jRaf, rawIO));
+      zipModel.setEndOfCentralDirectoryRecord(readEndOfCentralDirectoryRecord(zip4jRaf, rawIO, charset));
     } catch (ZipException e){
       throw e;
     } catch (IOException e) {
@@ -89,7 +94,7 @@ public class HeaderReader {
     return zipModel;
   }
 
-  private EndOfCentralDirectoryRecord readEndOfCentralDirectoryRecord(RandomAccessFile zip4jRaf, RawIO rawIO)
+  private EndOfCentralDirectoryRecord readEndOfCentralDirectoryRecord(RandomAccessFile zip4jRaf, RawIO rawIO, Charset charset)
       throws IOException {
     long pos = zip4jRaf.length() - ENDHDR;
 
@@ -123,7 +128,7 @@ public class HeaderReader {
     if (commentLength > 0) {
       byte[] commentBuf = new byte[commentLength];
       zip4jRaf.readFully(commentBuf);
-      endOfCentralDirectoryRecord.setComment(new String(commentBuf, InternalZipConstants.CHARSET_UTF_8));
+      endOfCentralDirectoryRecord.setComment(new String(commentBuf, charset));
     } else {
       endOfCentralDirectoryRecord.setComment(null);
     }


=====================================
src/main/java/net/lingala/zip4j/headers/HeaderWriter.java
=====================================
@@ -20,6 +20,7 @@ import net.lingala.zip4j.exception.ZipException;
 import net.lingala.zip4j.io.outputstream.CountingOutputStream;
 import net.lingala.zip4j.io.outputstream.SplitOutputStream;
 import net.lingala.zip4j.model.AESExtraDataRecord;
+import net.lingala.zip4j.model.ExtraDataRecord;
 import net.lingala.zip4j.model.FileHeader;
 import net.lingala.zip4j.model.LocalFileHeader;
 import net.lingala.zip4j.model.Zip64EndOfCentralDirectoryLocator;
@@ -202,7 +203,7 @@ public class HeaderWriter {
         writeZip64EndOfCentralDirectoryLocator(zipModel, byteArrayOutputStream, rawIO);
       }
 
-      writeEndOfCentralDirectoryRecord(zipModel, sizeOfCentralDir, offsetCentralDir, byteArrayOutputStream, rawIO);
+      writeEndOfCentralDirectoryRecord(zipModel, sizeOfCentralDir, offsetCentralDir, byteArrayOutputStream, rawIO, charset);
       writeZipHeaderBytes(zipModel, outputStream, byteArrayOutputStream.toByteArray(), charset);
     }
   }
@@ -236,7 +237,7 @@ public class HeaderWriter {
         writeZip64EndOfCentralDirectoryLocator(zipModel, byteArrayOutputStream, rawIO);
       }
 
-      writeEndOfCentralDirectoryRecord(zipModel, sizeOfCentralDir, offsetCentralDir, byteArrayOutputStream, rawIO);
+      writeEndOfCentralDirectoryRecord(zipModel, sizeOfCentralDir, offsetCentralDir, byteArrayOutputStream, rawIO, charset);
       writeZipHeaderBytes(zipModel, outputStream, byteArrayOutputStream.toByteArray(), charset);
     }
   }
@@ -435,13 +436,7 @@ public class HeaderWriter {
         System.arraycopy(longBuff, 0, offsetLocalHeaderBytes, 0, 4);
       }
 
-      int extraFieldLength = 0;
-      if (writeZip64ExtendedInfo) {
-        extraFieldLength += ZIP64_EXTRA_DATA_RECORD_SIZE_FH + 4; // 4 for signature + size of record
-      }
-      if (fileHeader.getAesExtraDataRecord() != null) {
-        extraFieldLength += AES_EXTRA_DATA_RECORD_SIZE;
-      }
+      int extraFieldLength = calculateExtraDataRecordsSize(fileHeader, writeZip64ExtendedInfo);
       rawIO.writeShortLittleEndian(byteArrayOutputStream, extraFieldLength);
 
       String fileComment = fileHeader.getFileComment();
@@ -496,6 +491,8 @@ public class HeaderWriter {
         rawIO.writeShortLittleEndian(byteArrayOutputStream, aesExtraDataRecord.getCompressionMethod().getCode());
       }
 
+      writeRemainingExtraDataRecordsIfPresent(fileHeader, byteArrayOutputStream);
+
       if (fileCommentBytes.length > 0) {
         byteArrayOutputStream.write(fileCommentBytes);
       }
@@ -504,6 +501,48 @@ public class HeaderWriter {
     }
   }
 
+  private int calculateExtraDataRecordsSize(FileHeader fileHeader, boolean writeZip64ExtendedInfo) throws IOException {
+    int extraFieldLength = 0;
+
+    if (writeZip64ExtendedInfo) {
+      extraFieldLength += ZIP64_EXTRA_DATA_RECORD_SIZE_FH + 4; // 4 for signature + size of record
+    }
+
+    if (fileHeader.getAesExtraDataRecord() != null) {
+      extraFieldLength += AES_EXTRA_DATA_RECORD_SIZE;
+    }
+
+    if (fileHeader.getExtraDataRecords() != null) {
+      for (ExtraDataRecord extraDataRecord : fileHeader.getExtraDataRecords()) {
+        if (extraDataRecord.getHeader() == HeaderSignature.AES_EXTRA_DATA_RECORD.getValue()
+            || extraDataRecord.getHeader() == HeaderSignature.ZIP64_EXTRA_FIELD_SIGNATURE.getValue()) {
+          continue;
+        }
+
+        extraFieldLength += 4 + extraDataRecord.getSizeOfData(); // 4  = 2 for header + 2 for size of data
+      }
+    }
+
+    return extraFieldLength;
+  }
+
+  private void writeRemainingExtraDataRecordsIfPresent(FileHeader fileHeader, OutputStream outputStream) throws IOException {
+    if (fileHeader.getExtraDataRecords() == null || fileHeader.getExtraDataRecords().size() == 0) {
+      return;
+    }
+
+    for (ExtraDataRecord extraDataRecord : fileHeader.getExtraDataRecords()) {
+      if (extraDataRecord.getHeader() == HeaderSignature.AES_EXTRA_DATA_RECORD.getValue()
+            || extraDataRecord.getHeader() == HeaderSignature.ZIP64_EXTRA_FIELD_SIGNATURE.getValue()) {
+        continue;
+      }
+
+      rawIO.writeShortLittleEndian(outputStream, (int) extraDataRecord.getHeader());
+      rawIO.writeShortLittleEndian(outputStream, extraDataRecord.getSizeOfData());
+      outputStream.write(extraDataRecord.getData());
+    }
+  }
+
   private void writeZip64EndOfCentralDirectoryRecord(ZipModel zipModel, int sizeOfCentralDir, long offsetCentralDir,
                                                      ByteArrayOutputStream byteArrayOutputStream, RawIO rawIO)
       throws IOException {
@@ -558,7 +597,7 @@ public class HeaderWriter {
   }
 
   private void writeEndOfCentralDirectoryRecord(ZipModel zipModel, int sizeOfCentralDir, long offsetCentralDir,
-                                                ByteArrayOutputStream byteArrayOutputStream, RawIO rawIO)
+                                                ByteArrayOutputStream byteArrayOutputStream, RawIO rawIO, Charset charset)
       throws IOException {
 
     byte[] longByte = new byte[8];
@@ -596,7 +635,7 @@ public class HeaderWriter {
 
     String comment = zipModel.getEndOfCentralDirectoryRecord().getComment();
     if (isStringNotNullAndNotEmpty(comment)) {
-      byte[] commentBytes = comment.getBytes(InternalZipConstants.CHARSET_UTF_8);
+      byte[] commentBytes = comment.getBytes(charset);
       rawIO.writeShortLittleEndian(byteArrayOutputStream, commentBytes.length);
       byteArrayOutputStream.write(commentBytes);
     } else {


=====================================
src/main/java/net/lingala/zip4j/io/inputstream/CipherInputStream.java
=====================================
@@ -4,6 +4,7 @@ import net.lingala.zip4j.crypto.Decrypter;
 import net.lingala.zip4j.exception.ZipException;
 import net.lingala.zip4j.model.LocalFileHeader;
 import net.lingala.zip4j.model.enums.CompressionMethod;
+import net.lingala.zip4j.util.InternalZipConstants;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -24,7 +25,7 @@ abstract class CipherInputStream<T extends Decrypter> extends InputStream {
     this.localFileHeader = localFileHeader;
 
     if (getCompressionMethod(localFileHeader) == CompressionMethod.DEFLATE) {
-      lastReadRawDataCache = new byte[512];
+      lastReadRawDataCache = new byte[InternalZipConstants.BUFF_SIZE];
     }
   }
 


=====================================
src/main/java/net/lingala/zip4j/io/inputstream/InflaterInputStream.java
=====================================
@@ -1,5 +1,7 @@
 package net.lingala.zip4j.io.inputstream;
 
+import net.lingala.zip4j.util.InternalZipConstants;
+
 import java.io.EOFException;
 import java.io.IOException;
 import java.io.PushbackInputStream;
@@ -16,7 +18,7 @@ public class InflaterInputStream extends DecompressedInputStream {
   public InflaterInputStream(CipherInputStream cipherInputStream) {
     super(cipherInputStream);
     this.inflater = new Inflater(true);
-    buff = new byte[512];
+    buff = new byte[InternalZipConstants.BUFF_SIZE];
   }
 
   @Override


=====================================
src/main/java/net/lingala/zip4j/io/inputstream/ZipInputStream.java
=====================================
@@ -70,7 +70,7 @@ public class ZipInputStream extends InputStream {
       charset = InternalZipConstants.CHARSET_UTF_8;
     }
 
-    this.inputStream = new PushbackInputStream(inputStream, 512);
+    this.inputStream = new PushbackInputStream(inputStream, InternalZipConstants.BUFF_SIZE);
     this.password = password;
     this.charset = charset;
   }


=====================================
src/test/java/net/lingala/zip4j/ExtractZipFileIT.java
=====================================
@@ -366,6 +366,16 @@ public class ExtractZipFileIT extends AbstractIT {
     testExtractNestedZipFileWithEncrpytion(EncryptionMethod.AES, EncryptionMethod.ZIP_STANDARD);
   }
 
+  @Test
+  public void testExtractZipFileLessThanMinimumExpectedZipFileSizeThrowsException() throws IOException {
+    expectedException.expect(ZipException.class);
+    expectedException.expectMessage("Zip file size less than minimum expected zip file size. " +
+        "Probably not a zip file or a corrupted zip file");
+
+    ZipFile zipFile = new ZipFile(getTestArchiveFromResources("invalid_zip_file_size_less_than_22kb.zip"));
+    zipFile.extractAll(temporaryFolder.toString());
+  }
+
   private void testExtractNestedZipFileWithEncrpytion(EncryptionMethod innerZipEncryption,
                                                        EncryptionMethod outerZipEncryption) throws IOException {
     File innerZipFile = temporaryFolder.newFile("inner.zip");


=====================================
src/test/java/net/lingala/zip4j/MiscZipFileIT.java
=====================================
@@ -18,6 +18,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -247,6 +248,30 @@ public class MiscZipFileIT extends AbstractIT {
     assertThat(zipFile.getComment()).isEqualTo("SOME_COMMENT");
   }
 
+  @Test
+  public void testSetCommentWithChineseCharacters() throws ZipException {
+    ZipFile zipFile = new ZipFile(generatedZipFile);
+    zipFile.setCharset(Charset.forName("GBK"));
+    zipFile.addFiles(FILES_TO_ADD);
+
+    zipFile.setComment("测试中文");
+
+    zipFile = new ZipFile(generatedZipFile);
+    zipFile.setCharset(Charset.forName("GBK"));
+    assertThat(zipFile.getComment()).isEqualTo("测试中文");
+  }
+
+  @Test
+  public void testSetCommentWithGermanCharacters() throws ZipException {
+    ZipFile zipFile = new ZipFile(generatedZipFile);
+    zipFile.addFiles(FILES_TO_ADD);
+
+    zipFile.setComment("ÄÜÖÖÜSDSDS");
+
+    zipFile = new ZipFile(generatedZipFile);
+    assertThat(zipFile.getComment()).isEqualTo("ÄÜÖÖÜSDSDS");
+  }
+
   @Test
   public void testSetCommentForMergedZipRetainsComment() throws ZipException {
     ZipFile zipFile = new ZipFile(generatedZipFile);


=====================================
src/test/java/net/lingala/zip4j/headers/HeaderReaderIT.java
=====================================
@@ -58,7 +58,7 @@ public class HeaderReaderIT extends AbstractIT {
     actualZipModel.setZipFile(headersFile);
 
     try(RandomAccessFile randomAccessFile = initializeRandomAccessFile(actualZipModel.getZipFile())) {
-      ZipModel readZipModel = headerReader.readAllHeaders(randomAccessFile, null);
+      ZipModel readZipModel = headerReader.readAllHeaders(randomAccessFile, InternalZipConstants.CHARSET_UTF_8);
       verifyZipModel(readZipModel, 1, false);
 
       EndOfCentralDirectoryRecord endOfCentralDirectoryRecord = readZipModel.getEndOfCentralDirectoryRecord();


=====================================
src/test/resources/test-archives/invalid_zip_file_size_less_than_22kb.zip
=====================================
@@ -0,0 +1 @@
+version=18.3.0



View it on GitLab: https://salsa.debian.org/java-team/zip4j/commit/6f0d423b8ad14a4d1e1efb3184b01aec506b97b7

-- 
View it on GitLab: https://salsa.debian.org/java-team/zip4j/commit/6f0d423b8ad14a4d1e1efb3184b01aec506b97b7
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20191230/d09997de/attachment.html>


More information about the pkg-java-commits mailing list