2

I'm trying to get input stream from zip file then add it to another zip file but it throws null pointer exception. here is my code.

       ZipParameters parameters = new ZipParameters();
       ZipFile newZipFile = new ZipFile(new File(CacheDir, "temp.card"));
       File temp = new File(CacheDir, "tempFile");
        if (!temp.exists()) {
                temp.createNewFile();
        }
       newZipFile.createZipFile(temp, parameters);
       if(newZipFile.getFile().exist()){
       if (string != null) {
            inputStream = zipFile.getInputStream(zipFile.getFileHeader(string));
       }
       if (inputStream != null) {
            newZipFile.addStream(inputStream,parameters);
       }
       }

non of objects in this scope are null. but I'm getting exception here:

newZipFile.addStream(inputStream,parameters);

Log:

12-14 06:27:43.891: W/System.err(2197): net.lingala.zip4j.exception.ZipException: input file is null
12-14 06:27:43.891: W/System.err(2197):     at net.lingala.zip4j.io.CipherOutputStream.putNextEntry(CipherOutputStream.java:71)
12-14 06:27:43.895: W/System.err(2197):     at net.lingala.zip4j.io.DeflaterOutputStream.putNextEntry(DeflaterOutputStream.java:45)
12-14 06:27:43.895: W/System.err(2197):     at net.lingala.zip4j.zip.ZipEngine.addStreamToZip(ZipEngine.java:230)
12-14 06:27:43.895: W/System.err(2197):     at net.lingala.zip4j.core.ZipFile.addStream(ZipFile.java:395)
  • If the offending line is `newZipFile.addStream(inputStream,parameters);`, then we should be able to assume `newZipFile`, `inputStream`, or `paramaters` are null. I see the initialization of `inputStream` in your snippet; however I do not see initialization of the other variables. Please post more code or that's the only assumption we can make. – MeetTitan Dec 14 '14 at 07:29

1 Answers1

4

Tracing that exception to its origin, I found in the source code of zip4j that

if (!zipParameters.isSourceExternalStream() && file == null) {
    throw new ZipException("input file is null");
}

So the quick fix is to bypass that check by

parameters.setSourceExternalStream(true);

P.S.: I have no idea what externalStream means, but it worked

Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57