2

I am trying to zip a bunch of files from different locations into one zip folder using Zip4J. I have an ArrayList of files from different folders, and as I loop through the list calling "zip.AddFile(file, params)" the files are added fine until the folder that the files are coming from switches. And I get the following error,

 net.lingala.zip4j.exception.ZipException: java.lang.NullPointerException
        at net.lingala.zip4j.zip.ZipEngine.initAddFiles(ZipEngine.java:187)
        at net.lingala.zip4j.zip.ZipEngine.addFiles(ZipEngine.java:85)
        at net.lingala.zip4j.core.ZipFile.addFiles(ZipFile.java:292)
        at net.lingala.zip4j.core.ZipFile.addFile(ZipFile.java:250)
        at tasks.OutputTask.compressFiles(OutputTask.java:627)
        at tasks.OutputTask.uploadHelper(OutputTask.java:550)
        at tasks.OutputTask.uploadHelper(OutputTask.java:593)
        at tasks.OutputTask.uploadHelper(OutputTask.java:593)
        at tasks.OutputTask.uploadHelper(OutputTask.java:593)
        at tasks.OutputTask.uploadHelper(OutputTask.java:593)
        at tasks.OutputTask.upload(OutputTask.java:516)
        at tasks.OutputTask.call(OutputTask.java:279)
        at tasks.OutputTask.call(OutputTask.java:1)
        at javafx.concurrent.Task$TaskCallable.call(Unknown Source)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException
        at net.lingala.zip4j.zip.ZipEngine.prepareFileOutputStream(ZipEngine.java:433)
        at net.lingala.zip4j.zip.ZipEngine.removeFilesIfExists(ZipEngine.java:385)
        at net.lingala.zip4j.zip.ZipEngine.initAddFiles(ZipEngine.java:109)
        ... 17 more

The code itself is

        ZipFile zip;
        try {
            System.out.println("Creating zip: " + outputName);
            File zipfile;
            if ((zipfile = new File(outputName)).exists()) {
                zipfile.delete();
            }
            zip = new ZipFile(new File(outputName));

            ZipParameters params = new ZipParameters();
            if (encrypt) {
                params.setEncryptFiles(true);
                params.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
                params.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
                params.setPassword(Property.AES_ENCRYPTION_KEY.getValue());

            }
            params.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            params.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            for (File file : sourceFiles) {
                if (file.isFile()) {
                    try {
                        zip.addFile(file, params);
                    } catch (ZipException e) {
                        e.printStackTrace();
                    }
                }
          }

So for example, here is the list of files

files = {C:/Folder1/img1, C/Folder1/txtFile, C:/Folder1/jar, C:/Folder2/img2}

the code will execute fine until it hits the "C:/Folder2/img2" where it will return the above error. I have tried

params.setSourceExternalStream(true);

which provides a different error, saying that there is no name for the external source file. Any help would be greatly appreciated. Thanks in advance

nsmith95
  • 51
  • 7

1 Answers1

0

Try adding params.setFileNameInZip(filename); params.setSourceExternalStream(true);

JoeH-Java
  • 126
  • 3