Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ZipInputStream: getting file size of -1 when reading

 
Stefan Evans
Bartender
Posts: 1717
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Can anyone shed some light on this?

Filename: works.zip
Description: Zip file which
File size: 207 bytes
[Download works.zip] Download Attachment
 
Randall Twede
Ranch Hand
Posts: 4396
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all i can tell you is that a return value of -1 is the same as saying "i cant find it" or null or false
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True. From the Javadocs of ZipEntry.getSize():
Returns:
the uncompressed size of the entry data, or -1 if not known

You'll need to take other measures if that's the case (like using a ByteArrayOutputStream).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic