Zip4J,Null指针由外部文件流引起的异常?

时间:2016-05-07 16:11:18

标签: java nullpointerexception zip compression zip4j

我正在尝试使用Zip4J将来自不同位置的一堆文件压缩到一个zip文件夹中。我有一个来自不同文件夹的文件的ArrayList,当我遍历列表调用" zip.AddFile(file,params)"文件被正确添加,直到文件来自交换机的文件夹。我收到以下错误,

 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

代码本身是

        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();
                    }
                }
          }

例如,这是文件列表

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

代码将执行正常,直到它到达" C:/ Folder2 / img2"它将返回上述错误。我试过了

params.setSourceExternalStream(true);

提供了不同的错误,表示外部源文件没有名称。任何帮助将不胜感激。提前致谢

1 个答案:

答案 0 :(得分:0)

尝试添加 params.setFileNameInZip(文件名); params.setSourceExternalStream(真);