zip4j is a great library. But i run into a problem when using it in a class that uses a thread. The zip4j method is called from a class that implements thread and sometimes (not always) it leaves files uncompress and somtimes there are leftofer files with the extension *.zip345. Also the process returns net.lingala.zip4j.exception.ZipException: cannot rename modified zip file.
The method zip4jProcess is called from the class public method. Class name is: SZipInterface.class
The SZipInterface.class
is initialized in the thread class ex: ThreadObj.class and instantiated per thread. No static method is used.
What is the cause of the problems? How do you fix it? Is zip4j thread safe?
Method:
private int zip4jProcess() {
int status = 0;
if (null != getInFiles() && getInFiles().length > 0) {
for (String file : getInFiles()) {
File sourceFile = new File(file);
ZipFile zipFile = null;
ZipParameters zipParams = new ZipParameters();
if (getPassword() != null
&& !getPassword().trim().equalsIgnoreCase("")) {
zipParams.setPassword(getPassword());
zipParams.setEncryptFiles(true);
zipParams
.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
}
zipParams
.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
if (sourceFile.exists()) {
try {
zipFile = new ZipFile(getZipFileName());
if (zipFile.getFile().exists()) {
zipFile.addFile(sourceFile, zipParams);
if (log.isDebugEnabled()) {
log.debug("Adding: " + sourceFile.getName()
+ " to " + zipFile.getFile().getName()
+ " Pass: " + getPassword());
}
} else {
zipFile.createZipFile(sourceFile, zipParams);
if (log.isDebugEnabled()) {
log.debug("Creating: " + sourceFile.getName()
+ " to " + zipFile.getFile().getName()
+ " Pass: " + getPassword());
}
}
} catch (ZipException e) {
log.error(e);
status = 1;
}
}
}
}
return status;
}