Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add deflate64 (deflate enhanced) support #526

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
</distributionManagement>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.25.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
35 changes: 21 additions & 14 deletions src/main/java/net/lingala/zip4j/headers/FileHeaderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,27 @@ private byte generateFirstGeneralPurposeByte(boolean isEncrypted, ZipParameters
firstByte = setBit(firstByte, 0);
}

if (CompressionMethod.DEFLATE.equals(zipParameters.getCompressionMethod())) {
if (CompressionLevel.NORMAL.equals(zipParameters.getCompressionLevel())) {
firstByte = unsetBit(firstByte, 1);
firstByte = unsetBit(firstByte, 2);
} else if (CompressionLevel.MAXIMUM.equals(zipParameters.getCompressionLevel())) {
firstByte = setBit(firstByte, 1);
firstByte = unsetBit(firstByte, 2);
} else if (CompressionLevel.FAST.equals(zipParameters.getCompressionLevel())) {
firstByte = unsetBit(firstByte, 1);
firstByte = setBit(firstByte, 2);
} else if (CompressionLevel.FASTEST.equals(zipParameters.getCompressionLevel())
|| CompressionLevel.ULTRA.equals(zipParameters.getCompressionLevel())) {
firstByte = setBit(firstByte, 1);
firstByte = setBit(firstByte, 2);
if (zipParameters.getCompressionMethod() != null) {
switch (zipParameters.getCompressionMethod()) {
case DEFLATE:
case DEFLATE64:
if (CompressionLevel.NORMAL.equals(zipParameters.getCompressionLevel())) {
firstByte = unsetBit(firstByte, 1);
firstByte = unsetBit(firstByte, 2);
} else if (CompressionLevel.MAXIMUM.equals(zipParameters.getCompressionLevel())) {
firstByte = setBit(firstByte, 1);
firstByte = unsetBit(firstByte, 2);
} else if (CompressionLevel.FAST.equals(zipParameters.getCompressionLevel())) {
firstByte = unsetBit(firstByte, 1);
firstByte = setBit(firstByte, 2);
} else if (CompressionLevel.FASTEST.equals(zipParameters.getCompressionLevel())
|| CompressionLevel.ULTRA.equals(zipParameters.getCompressionLevel())) {
firstByte = setBit(firstByte, 1);
firstByte = setBit(firstByte, 2);
}
break;
default:
// do nothing
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.lingala.zip4j.crypto.Decrypter;
import net.lingala.zip4j.model.LocalFileHeader;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.util.Zip4jUtil;

import java.io.IOException;
Expand All @@ -24,8 +23,12 @@ public CipherInputStream(ZipEntryInputStream zipEntryInputStream, LocalFileHeade
this.decrypter = initializeDecrypter(localFileHeader, password, useUtf8ForPassword);
this.localFileHeader = localFileHeader;

if (Zip4jUtil.getCompressionMethod(localFileHeader).equals(CompressionMethod.DEFLATE)) {
lastReadRawDataCache = new byte[bufferSize];
switch (Zip4jUtil.getCompressionMethod(localFileHeader)) {
case DEFLATE:
case DEFLATE64:
lastReadRawDataCache = new byte[bufferSize];
break;
default:
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.lingala.zip4j.io.inputstream;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

import org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream;

public class Inflater64InputStream extends DecompressedInputStream {

private Deflate64CompressorInputStream inflater;
private BufferedInputStream buffer;

public Inflater64InputStream(CipherInputStream<?> cipherInputStream, int bufferSize) {
super(cipherInputStream);
this.inflater = new Deflate64CompressorInputStream(cipherInputStream);
this.buffer = new BufferedInputStream(this.inflater, bufferSize);
}

@Override
public int read() throws IOException {
return this.buffer.read();
}

@Override
public int read(byte[] b) throws IOException {
return this.buffer.read(b);
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
return this.buffer.read(b, off, len);
}

@Override
public void endOfEntryReached(InputStream inputStream, int numberOfBytesPushedBack) throws IOException {
if (inflater != null) {
buffer.close();
inflater.close(); // this is redundant, but leaves no doubt for readability
inflater = null;
buffer = null;
}
super.endOfEntryReached(inputStream, numberOfBytesPushedBack);
}

@Override
public int pushBackInputStreamIfNecessary(PushbackInputStream pushbackInputStream) throws IOException {
// Deflate64CompressorInputStream should not overread
return 0;
}

@Override
public void close() throws IOException {
if (inflater != null) {
buffer.close();
inflater.close(); // this is redundant, but leaves no doubt for readability
}
super.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,15 @@ private DecompressedInputStream initializeDecompressorForThisEntry(CipherInputSt
LocalFileHeader localFileHeader) throws ZipException {
CompressionMethod compressionMethod = getCompressionMethod(localFileHeader);

if (compressionMethod == CompressionMethod.DEFLATE) {
return new InflaterInputStream(cipherInputStream, zip4jConfig.getBufferSize());
if (compressionMethod != null) {
switch (compressionMethod) {
case DEFLATE:
return new InflaterInputStream(cipherInputStream, zip4jConfig.getBufferSize());
case DEFLATE64:
return new Inflater64InputStream(cipherInputStream, zip4jConfig.getBufferSize());
default:
// do nothing
}
}

return new StoreInputStream(cipherInputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,16 @@ private CipherOutputStream<?> initializeCipherOutputStream(ZipEntryOutputStream
}

private CompressedOutputStream initializeCompressedOutputStream(CipherOutputStream<?> cipherOutputStream,
ZipParameters zipParameters) {
if (zipParameters.getCompressionMethod() == CompressionMethod.DEFLATE) {
return new DeflaterOutputStream(cipherOutputStream, zipParameters.getCompressionLevel(), zip4jConfig.getBufferSize());
ZipParameters zipParameters) throws ZipException {
if (zipParameters.getCompressionMethod() != null) {
switch (zipParameters.getCompressionMethod()) {
case DEFLATE:
return new DeflaterOutputStream(cipherOutputStream, zipParameters.getCompressionLevel(), zip4jConfig.getBufferSize());
case DEFLATE64:
throw new ZipException("Deflate64 not supported for compression");
default:
// do nothing
}
}

return new StoreOutputStream(cipherOutputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public enum CompressionMethod {
* @see java.util.zip.Deflater
*/
DEFLATE(8),
/**
* The Deflate enhanced or deflate64 compression is used.
* @see org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream
*/
DEFLATE64(9),
/**
* For internal use in Zip4J
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import static net.lingala.zip4j.headers.HeaderUtil.getFileHeader;
import static net.lingala.zip4j.model.ZipParameters.SymbolicLinkAction.INCLUDE_LINK_AND_LINKED_FILE;
import static net.lingala.zip4j.model.ZipParameters.SymbolicLinkAction.INCLUDE_LINK_ONLY;
import static net.lingala.zip4j.model.enums.CompressionMethod.DEFLATE;
import static net.lingala.zip4j.model.enums.CompressionMethod.AES_INTERNAL_ONLY;
import static net.lingala.zip4j.model.enums.CompressionMethod.STORE;
import static net.lingala.zip4j.model.enums.EncryptionMethod.NONE;
import static net.lingala.zip4j.model.enums.EncryptionMethod.ZIP_STANDARD;
Expand Down Expand Up @@ -175,7 +175,7 @@ void verifyZipParameters(ZipParameters parameters) throws ZipException {
throw new ZipException("cannot validate zip parameters");
}

if (parameters.getCompressionMethod() != STORE && parameters.getCompressionMethod() != DEFLATE) {
if (parameters.getCompressionMethod() == null || parameters.getCompressionMethod() == AES_INTERNAL_ONLY) {
throw new ZipException("unsupported compression type");
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/lingala/zip4j/util/ZipVersionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static int determineVersionMadeBy(ZipParameters zipParameters, RawIO rawI
public static VersionNeededToExtract determineVersionNeededToExtract(ZipParameters zipParameters) {
VersionNeededToExtract versionRequired = VersionNeededToExtract.DEFAULT;

if (zipParameters.getCompressionMethod() == CompressionMethod.DEFLATE) {
if (zipParameters.getCompressionMethod() == CompressionMethod.DEFLATE ||
zipParameters.getCompressionMethod() == CompressionMethod.DEFLATE64) {
versionRequired = VersionNeededToExtract.DEFLATE_COMPRESSED;
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/java/net/lingala/zip4j/ExtractZipFileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,14 @@ public void testExtractZipFileCRCError() throws IOException {
new File(outputFolder, "images"));
}

@Test
public void testExtractZipFileDeflate64() throws IOException {
ZipFile zipFile = new ZipFile(getTestArchiveFromResources("archive_deflate64_1_file.zip"));
zipFile.extractAll(outputFolder.getPath());
assertThat(outputFolder.listFiles()).contains(
new File(outputFolder, "test-simple.pdf"));
}

@Test
public void testExtractZipFileOf7ZipFormatSplitWithoutEncryption() throws IOException {
List<File> filesToAddToZip = new ArrayList<>(FILES_TO_ADD);
Expand Down
Binary file not shown.