簡體   English   中英

Zip4j:如果存在已創建的zip文件,如何覆蓋

[英]Zip4j: How to overwrite if created zip file is exist

使用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);

希望這會有所幫助,問候

//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);
}

無論文件是否已經存在,以下代碼都將起作用:

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

如果刪除了文件,則布爾true如果文件不存在或無法刪除,則布爾true false 當然,如果由於“文件不存在”以外的其他原因無法刪除文件,您將不會知道。 如果您關心它,則應使用Arno_Geismar建議的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM