0

I'm trying to zip a bunch of files using the Zip4j library. I pass a list of the file paths of the files I want to compress and I add them one by one into the zip file. For some reason, the last file does not get added. I checked the indexes of the loop and I'm pretty sure they're correct. I am not getting any exceptions or error messages. Here's my code:

    // get the path; paths refers to the list of files to compress
    String uuidString = UUID.randomUUID().toString();
    String path = "H:/public/ZipFiles/" + uuidString + ".zip";

    try {
        // create the new zip file
        ZipFile zipFile = new ZipFile(path);
        File fileToAdd;
        String message = "";

        ZipParameters parameters = new ZipParameters();
        // set compression method to store compression
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);

        // Set the compression level
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);

        // add each file to the zipFile
        for(int i = 0; i < paths.size(); i++)
        {
            fileToAdd = new File(paths.get(i));

            if(fileToAdd.exists())
            {
                System.out.println("writing file at " + paths.get(i) + " to the zip file");
                zipFile.addFile(fileToAdd, parameters);
            }
            else
            {
                message += "File with at path " + paths.get(i) + " was not found.\n";
            }
        }

    } catch (ZipException e) {
        e.printStackTrace();
    }

All the file paths get printed when they are added. Any ideas?

tbodt
  • 16,609
  • 6
  • 58
  • 83

2 Answers2

0

You're not closing the ZipFile.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • The ZipFile class I'm using is not the Java.util.zip one, it is from the Zip4J library. The class I'm using does not have a method to close a ZipFile. – user2200996 Oct 07 '13 at 16:08
0

I think there is a problem with the jar file from their own website at http://www.lingala.net/zip4j/download.php

But when I downloaded it from the maven repository at https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j/1.3.2 , it is working perfectly.

Wilson
  • 1,259
  • 11
  • 17