zip4j 是一个很棒的库。但是我在使用线程的类中使用它时遇到了问题。zip4j 方法是从实现线程的类中调用的,有时(并非总是)它会使文件解压缩,有时会有扩展名为 *.zip345 的leftofer 文件。该过程还返回 net.lingala.zip4j.exception.ZipException: cannot rename modified zip 文件。
方法 zip4jProcess 是从类公共方法中调用的。班级名称是:SZipInterface.class
在SZipInterface.class
线程类中初始化,例如:ThreadObj.class 并在每个线程中实例化。没有使用静态方法。
问题的原因是什么?你如何解决它?zip4j 线程安全吗?
方法:
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;
}