[Kaffe] Help!, ZipEntry confusion.
Moses DeJong
dejong at cs.umn.edu
Sun Feb 14 14:10:17 PST 1999
Hello all,
I am currently working on some of the java.util.zip classes but I am
hopelessly stuck. I can not figure out how one is supposed to use the
ZipOutputStream and ZipEntry objects to write out uncompressed
(STORED) data streams. Writing of compressed (DEFLATED) streams works
as expected, but there seems to be a catch-22 in the API for uncompressed
streams. I have included a small example that shows the problem below.
The real issue here is that the ZipOutputStream requires that both the
crc and size values of a ZipEntry are set before and output stream is
written. This is not possible because you would not know how much data
was going to be written before you started writing it. I am also stumped
on this crc issue. The ZipEntry requires setting the crc for uncompressed
streams before the stream is actually written to the ZipOutputStream.
Again, this is a catch-22 as you would not know the crc until after
the stream was read. This crc thing is also odd because the Kaffe
implementation already calculates the crc inside the ZipOutputStream class.
Why on earth would I as the user of this class need to calculate it again?
Does anyone know what is going on here? I was not able to find any info
about this in the JDK docs and I do not know where else to look.
// File ZipEntryError.java
import java.io.*;
import java.util.zip.*;
public class ZipEntryError {
public static void main(String[] argv) throws Exception {
boolean compressed = true;
if (argv.length != 0)
compressed = false;
StringBufferInputStream sbis =
new StringBufferInputStream("file contents");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(baos);
ZipEntry ze = new ZipEntry("temp");
if (compressed) {
ze.setMethod(ZipEntry.DEFLATED);
} else {
ze.setMethod(ZipEntry.STORED);
// HACK!, we have no way to know these at this point, but
// not setting them will generate this error in putNextEntry().
// java.util.zip.ZipException:
// STORED entry missing size, compressed size, or crc-32
ze.setSize( 0 );
ze.setCrc( 0 );
}
zout.putNextEntry(ze);
// Read data from input stream to output stream
int data;
int bytes = 0;
while ((data = sbis.read()) != -1) {
zout.write(data);
bytes++;
}
// Update the size of the entry after reading the stream
ze.setSize( bytes );
// Close the current ZipEntry
zout.closeEntry();
// Close the entire Zip stream
zout.close();
// Find out how many bytes were output
byte[] output = baos.toByteArray();
System.out.println("output array has " + output.length + " bytes");
}
}
JDK 1.1 (with compression)
% java ZipEntryError
output array has 137 bytes
JDK 1.2 (with compression)
% java ZipEntryError
output array has 137 bytes
JDK 1.1 (no compression)
java ZipEntryError 1
java.util.zip.ZipException: attempt to write past end of STORED entry
at java.util.zip.ZipOutputStream.write(ZipOutputStream.java:259)
JDK 1.2 (no compression)
% java ZipEntryError 1
Exception in thread "main" java.util.zip.ZipException: attempt to write past end of STORED entry
at java.util.zip.ZipOutputStream.write(Compiled Code)
Kaffe (with compression)
% kaffe ZipEntryError
output array has 121 bytes
Kaffe (no compression)
% kaffe ZipEntryError 1
java.util.zip.ZipException: compress size set incorrectly
at java.util.zip.ZipOutputStream.closeEntry(ZipOutputStream.java:117)
thanks
Mo DeJong
dejong at cs.umn.edu
More information about the kaffe
mailing list