0

I want to overwrite existing zip file when I create zip file by using Zip4j. When I create zip file by using Zip4j, my file will split according to splitSize. So I can't check it. Here my Code sample ...

        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);
Aung Myat Hein
  • 4,018
  • 1
  • 36
  • 42

2 Answers2

1

Whether the file already exists or not, the following code will work:

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

The boolean will be true if the file was deleted, and false if the file did not exist or could not be deleted. Of course if the file could not be deleted for any reason other than "file does not exist", you will not know. If you care about it, you should use the code suggested by Arno_Geismar.

lafarer
  • 112
  • 4
0

Hope this helps, regards

//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);
}
Arno_Geismar
  • 2,296
  • 1
  • 15
  • 29
  • Thanks Arno .. When I create zip file by using Zip4j, my file will split according to splitSize. So I can't checkt it. – Aung Myat Hein Jul 10 '14 at 09:07
  • 1)create a directory for each zip file so that the splitted files are located in it. 2)if you are zipping the same file delete that directory – Arno_Geismar Jul 10 '14 at 09:15