-2

这是我的代码,但它给出了一个例外。

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;
}
4

2 回答 2

0

确保在创建文件后关闭文件,并在尝试删除成员之前重新打开它。否则,文件将保持锁定状态,并且在成员删除操作期间无法删除和重新创建。

于 2018-01-18T07:15:53.887 回答
0

我得到了答案,这种方法在进一步使用之前错过了关闭流。

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;
}
于 2018-01-18T07:16:10.853 回答