Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Bug in java.util.zip?

807591Apr 4 2008 — edited Apr 5 2008
Hi, I have some code that uses java.util.zip to put "MyFile" into "My.zip" file (using ZipOutputStream, etc). When I use java.util.zip to reopen the zip file, I run into a problem.

I open a ZipInputStream and call getNextEntry(), and get a ZipEntry for "MyFile" file I zipped. I then call ZipEntry.getSize() but it returns -1 (size unknown). But if I create a ZipFile object for the zip and call getEntry("MyFile"), when I call ZipEntry.getSize() it returns the correct size.

I thought it may be reasonable that ZipInputStream does not know the size of a zipped entry in a file (until it had read through/unpacked the object). However if I create the "My.zip" file with either Winzip or Winrar, when I open it with ZipInputStream, call getNextEntry() then ZipEntry.getSize(), it DOES return the correct size.

So why can I get the size of a ZipEntry (via ZipInputStream) from a zip created with Winzip, but not a zip created with java.util.zip?

*******

I tried using the ZipFile class (which gives me the entry size correctly) together with ZipInputStream to read "MyFile", so this seemed like a workaround. I have used this technique for one part of the project.
However, the above example is a simplification of what I'm actually trying to do in another part of the project. I am unzipping an entire jar file (11MB), replacing 3 class files and zipping it up again (a type of incremental patch/upgrade system). After the jar is recreated, it no longer works (an 'unable to load main class' exception occurs). If I open the jar with winzip, the class is there, has the right size and package path, the manifest is there, etc.
If I open the rebuilt jar with my sample program, using ZipInputStream, getNextEntry() then ZipEntry.getSize(), it returns -1 (as expected). I'm wondering - is this what the JVM/Classloader is attempting to do also, which is why it's failing to find (or load) the main class??
As a test, I took the rebuilt jar, extracted all with winzip then built it all into a new zip (and renamed to a jar), then it once again worked. Also to note - I've tried changing the java.util.zip api calls to JarEntry, JarOutputStream, etc, but this has the same problem.

So can anyone help? Is the java.util.zip api not producing zips quite right? (Winzip seems to work better.) Or did I miss something?
I have a couple of small test cases if anyone is interested - I can post here if anyone wants them.

Thanks in advance,
Phil

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 3 2008
Added on Apr 4 2008
7 comments
1,674 views