Java Zip4j:如果创建的zip文件存在,如何覆盖
我想在使用Zip4j创建zip文件时覆盖现有的zip文件。当我使用Zip4j创建zip文件时,我的文件将根据splitSize进行分割。所以我不能检查它。这里是我的代码示例Java Zip4j:如果创建的zip文件存在,如何覆盖,java,zip4j,Java,Zip4j,我想在使用Zip4j创建zip文件时覆盖现有的zip文件。当我使用Zip4j创建zip文件时,我的文件将根据splitSize进行分割。所以我不能检查它。这里是我的代码示例 File file = new File("C:\\temp\\5.pdf"); ZipParameters parameters = new ZipParameters(); // set compression method to store compression
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();
File File File=new File(“为什么不检查它是否存在并删除它?提供有关代码的信息。这很好,我想要草莓冰淇淋谢谢Arno..当我使用Zip4j创建zip文件时,我的文件将根据拆分大小进行拆分。所以我无法检查它。1)为每个zip文件创建一个目录,以便拆分的文件位于其中。2)如果要压缩同一个文件,请删除该目录