wood burning stoves 2.0*
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 Java 8 in Action this week in the Java 8 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
Author

ZipInputStream: getting file size of -1 when reading

Stefan Evans
Bartender

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 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?



[Download works.zip] Download

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

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


SCJP
Visit my download page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

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).


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