0

当我使用 Zip4j 创建 zip 文件时,我想覆盖现有的 zip 文件。当我使用 Zip4j 创建 zip 文件时,我的文件将根据 splitSize 进行拆分。所以我无法检查。这是我的代码示例...

        File file = new File("C:\\temp\\5.pdf");
        ZipParameters parameters = new ZipParameters();
        // set compression method to store compression
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
        // Set the compression level. This value has to be in between 0 to 9
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);


        zipFile.createZipFile(file, parameters, true, splitSize);
4

2 回答 2

1

无论文件是否已经存在,以下代码都将起作用:

File file = new File("<your zip file">);
boolean delete = file.delete();

true如果文件被删除,以及false文件不存在或无法删除,布尔值将是。当然,如果除了“文件不存在”之外的任何原因无法删除文件,您将不知道。如果你关心它,你应该使用 Arno_Geismar 建议的代码。

于 2014-07-10T09:15:21.260 回答
1

希望这会有所帮助,问候

//step 1

Path p1 = ...; //path to your potentially existing file
Path p2 = ...; //path of your new file;

if (Files.isSameFile(p1, p2)) {
  try {
  delete(p1) // delete the directory where your duplicate is located

  //step 2: insert your saving logic with zip4j ( make sure you create a new           subdirectory for each file you zip
  //step 3: eat some strawberry ice-cream 

  } catch (NoSuchFileException x) {
  System.err.format("%s: no such" + " file or directory%n", path);
  } catch (DirectoryNotEmptyException x) {
  System.err.format("%s not empty%n", path);
  } catch (IOException x) {
  // File permission problems are caught here.
  System.err.println(x);
  }
}

//use this method to delete the files in the directory before deleting it.
void delete(File f) throws IOException {
  if (f.isDirectory()) {
    for (File c : f.listFiles())
      delete(c);
  }
  if (!f.delete())
  throw new FileNotFoundException("Failed to delete file: " + f);
}
于 2014-07-10T08:52:40.377 回答