首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zip4j:如果存在创建的zip文件,如何覆盖

Zip4j:如果存在创建的zip文件,如何覆盖
EN

Stack Overflow用户
提问于 2014-07-10 08:34:06
回答 2查看 2.9K关注 0票数 0

当我使用Zip4j创建zip文件时,我希望覆盖现有的zip文件。当我使用Zip4j创建zip文件时,我的文件将根据splitSize进行拆分。所以我不能检查。这是我的密码样本..。

代码语言:javascript
复制
        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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-10 08:52:40

希望这能帮上忙

代码语言:javascript
复制
//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);
}
票数 0
EN

Stack Overflow用户

发布于 2014-07-10 09:15:21

无论文件是否已经存在,下列代码都将工作:

代码语言:javascript
复制
File file = new File("<your zip file">);
boolean delete = file.delete();

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24671851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档