This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes ZipInputStream: getting file size of -1 when reading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ZipInputStream: getting file size of -1 when reading" Watch "ZipInputStream: getting file size of -1 when reading" New topic

ZipInputStream: getting file size of -1 when reading

Stefan Evans

Joined: Jul 06, 2005
Posts: 1016
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 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?

[Download] Download

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340

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

Visit my download page
Rob Spoor

Joined: Oct 27, 2005
Posts: 19656

True. From the Javadocs of ZipEntry.getSize():
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).

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: ZipInputStream: getting file size of -1 when reading
Similar Threads
Windows XP Compressed Folders doesn't recognize my Java-created zip file
After Unzip delete that file programatically??
how to split a Zip file
Java --- ZIP & UNZIP
Not able to put some files inside Zip file