ZipInputStream: getting file size of -1 when reading
Joined: Jul 06, 2005
Problem in a nutshell:
Created Zip file with java.util.Zip
When reading the file using a ZipInputStream, ZipEntry objects are returned with their size = -1.
Scenario: We have an import process that takes a zip file. I was thinking to automate the production of this zip file with a simple java app.
Simple enough using the java.util.zip package, or so I thought.
File generated successfully, openable in jZip in the format I wanted. Excellent.
Unfortunately the process fails because the size of the first file in the archive gets returned as -1, which causes a NegativeArraySizeException (it tries to create a byte array the size of the file. Don't ask me why)
Whats even crazier is if I unzip the contents, and then zip with jZip, the resulting file completes processing successfully!
Obviously something is funny about the way I'm constructing the zip file - but what?
Here is the relevant code snippet:
I'm running java 1.6.0_22 in Eclipse.
The following test file demonstrates this.
It creates a zip file, and then tries to read it in three different ways. Two of them work, unfortunately the third (the same method used by the library) does not.
For each method it prints out the Zip Entry, and the 'size' of the Zip entry according to the object.
I've also attached a zip file which 'works' with all three methods.
The results I get from running this:
When I read my generated file using the ZipFile object it works, but not with ZipInputStream.
Reading the zip file generated with jZip works with all methods.
What I want is to be able to produce a zip file that can read the size back on the ZipInputStream.