如何使用 zip4j 从密码保护的 Zip 文件中删除文件?得到例外
how to remove file from password protected Zip File using zip4j ? getting exception
这是我的代码,但它给出了一个例外。
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") // here i'm
geting exception
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;
}
确保在创建文件后将其关闭,并在尝试删除成员之前重新打开它。否则文件将保持锁定状态,无法在成员删除操作期间删除和重新创建。
我得到了答案,这个方法在进一步使用之前没有关闭流。
public String getFileFromZip(String zipPath, String filePath) {
String data = null;
ZipInputStream is = null;
ZipFile zipFile = null;
try {
// Initiate the 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) {
System.err.println(getStringFromStackTrace(e));
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
}
}
return data;
}
这是我的代码,但它给出了一个例外。
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") // here i'm geting exception
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;
}
确保在创建文件后将其关闭,并在尝试删除成员之前重新打开它。否则文件将保持锁定状态,无法在成员删除操作期间删除和重新创建。
我得到了答案,这个方法在进一步使用之前没有关闭流。
public String getFileFromZip(String zipPath, String filePath) {
String data = null;
ZipInputStream is = null;
ZipFile zipFile = null;
try {
// Initiate the 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) {
System.err.println(getStringFromStackTrace(e));
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
}
}
return data;
}