当我使用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);发布于 2014-07-10 08:52:40
希望这能帮上忙
//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-10 09:15:21
无论文件是否已经存在,下列代码都将工作:
File file = new File("<your zip file">);
boolean delete = file.delete();如果文件被删除,则布尔值将为true,如果文件不存在或无法删除,则为false。当然,如果由于“文件不存在”以外的任何原因无法删除该文件,您将不知道。如果您关心它,您应该使用Arno_Geismar建议的代码。
https://stackoverflow.com/questions/24671851
复制相似问题