2

我正在使用 TrueZip 7 创建一个 tzp 文件,并使用 cp_rp 方法将所有目录内容一次添加到 tzp 文件中。

之后,我尝试将 tzp 文件的所有内容提取到目标目录。但是,调用:

zipFile = new TFile("test.zip");
public void extract(TFile file){
  for (TFile iFile : zipFile.listFiles()){
    if(iFile.isDirectory()){
       extract(iFile);
    }else{
       File output = new File(iFile.getPath());
       iFile.mv(output);
    }
  }
}

失败并出现错误:java.io.IOException: [path]\test.zip\Myjar.jar (destination exists already)。如果我使用 cp 而不是 mv,那么错误是[path]\test.zip\Myjar.jar(包含在 [path]\test.zip\Myjar.jar 中)

问题似乎还在于 TrueZip 将 zip 和 jar 识别为目录和档案,因此在对它们执行 isDirectory() 时,这会成功,并且执行 listFiles() 会返回其中包含的所有文件,因此在运行 cp() files 递归地导致所有 jar/zip 内容被复制为目录。

提取这些存档文件而不扩展它们的正确方法是什么?

4

1 回答 1

5

可以通过一个方法调用将存档文件提取到目录中:

TFile archive = new TFile("archive.zip");
TFile directory = new TFile("directory");
TFile.cp_rp(archive, directory, TArchiveDetector.NULL, TArchiveDetector.NULL);

诀窍是在遍历目录树时使用 TArchiveDetector.NULL。

于 2011-06-18T07:29:06.927 回答