Skip to content

Commit

Permalink
#112 Fix performance issues
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed Dec 4, 2019
1 parent c4f226d commit 6575edf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.2.7-SNAPSHOT</version>
<version>2.2.7.1-SNAPSHOT</version>

<name>Zip4j</name>
<description>Zip4j - A Java library for zip files and streams</description>
Expand Down
31 changes: 12 additions & 19 deletions src/main/java/net/lingala/zip4j/ZipFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void addFiles(List<File> filesToAdd, ZipParameters parameters) throws Zip

assertFilesExist(filesToAdd);

checkZipModel();
readZipInfo();

if (zipModel == null) {
throw new ZipException("internal error: zip model is null");
Expand Down Expand Up @@ -350,7 +350,7 @@ public void addFolder(File folderToAdd, ZipParameters zipParameters) throws ZipE
*/
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");
Expand Down Expand Up @@ -389,7 +389,7 @@ public void addStream(InputStream inputStream, ZipParameters parameters) throws

this.setRunInThread(false);

checkZipModel();
readZipInfo();

if (zipModel == null) {
throw new ZipException("internal error: zip model is null");
Expand Down Expand Up @@ -696,7 +696,7 @@ public void mergeSplitFiles(File outputZipFile) throws ZipException {
throw new ZipException("output Zip File already exists");
}

checkZipModel();
readZipInfo();

if (this.zipModel == null) {
throw new ZipException("zip model is null, corrupt zip file?");
Expand Down Expand Up @@ -746,7 +746,7 @@ public String getComment() throws ZipException {
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");
Expand All @@ -773,7 +773,7 @@ public ZipInputStream getInputStream(FileHeader fileHeader) throws IOException {
throw new ZipException("FileHeader is null, cannot get InputStream");
}

checkZipModel();
readZipInfo();

if (zipModel == null) {
throw new ZipException("zip model is null, cannot get inputstream");
Expand Down Expand Up @@ -813,7 +813,7 @@ public boolean isValidZipFile() {
* @throws ZipException
*/
public List<File> getSplitZipFiles() throws ZipException {
checkZipModel();
readZipInfo();
return FileUtils.getSplitZipFiles(zipModel);
}

Expand All @@ -827,12 +827,16 @@ public void setPassword(char[] password) {

/**
* 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;
Expand All @@ -853,17 +857,6 @@ private void readZipInfo() throws ZipException {
}
}

/**
* 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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand All @@ -24,7 +25,7 @@ public CipherInputStream(ZipEntryInputStream zipEntryInputStream, LocalFileHeade
this.localFileHeader = localFileHeader;

if (getCompressionMethod(localFileHeader) == CompressionMethod.DEFLATE) {
lastReadRawDataCache = new byte[512];
lastReadRawDataCache = new byte[InternalZipConstants.BUFF_SIZE];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public ZipInputStream(InputStream inputStream, char[] password, Charset charset)
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;
}
Expand Down

0 comments on commit 6575edf

Please sign in to comment.