Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

ZipInputStream: getting file size of -1 when reading

Posts: 1845
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 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?

Description: Zip file which
File size: 207 bytes
Ranch Hand
Posts: 4716
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22563
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic