这是我的代码,但它给出了一个例外。
net.lingala.zip4j.exception.ZipException: cannot delete old zip file
at net.lingala.zip4j.util.ArchiveMaintainer.restoreFileName(ArchiveMaintainer.java:234)
at net.lingala.zip4j.util.ArchiveMaintainer.initRemoveZipFile(ArchiveMaintainer.java:216)
at net.lingala.zip4j.util.ArchiveMaintainer.removeZipFile(ArchiveMaintainer.java:61)
at net.lingala.zip4j.core.ZipFile.removeFile(ZipFile.java:821)
at net.lingala.zip4j.core.ZipFile.removeFile(ZipFile.java:794)
at com.imimobile.workflow.zip.poc.ZipUtility.removeFileFromZipFile(ZipUtility.java:50)
at com.imimobile.workflow.zip.poc.ProcessExc.main(ProcessExc.java:25)
public boolean removeFileFromZipFile(String zipFilepath, String filepath) {
try {
ZipFile zipFile = new ZipFile(zipFilepath);
zipFile.removeFile(filepath);
} catch (ZipException ex) {
ex.printStackTrace();
return false;
}
return true;
}
如果我调用单个方法,则此代码有效
removeFileFromZipFile("E:\POC\files\test.zip", "test.html")
当我尝试按顺序执行以下操作时,它不起作用。
- 从 zip 中读取文件
- 从 zip 中删除文件
getFileFromZip("E:\POC\files\test.zip", "test.html") removeFileFromZipFile("E:\POC\files\test.zip", "test.html") // 这里出现异常
public String getFileFromZip(String zipPath, String filePath) {
String data = null;
ZipInputStream is = null;
try {
// Initiate the ZipFile
ZipFile zipFile = new ZipFile(zipPath);
if (zipFile.isEncrypted()) {
zipFile.setPassword("abc@123");
}
FileHeader fileHeader = zipFile.getFileHeader(filePath);
if (fileHeader != null) {
is = zipFile.getInputStream(fileHeader);
data = IOUtils.toString(is, StandardCharsets.UTF_8);
System.out.println("Data :: " + data);
} else {
System.err.println("FileHeader does not exist");
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}