ZipEntry.DEFLATED

I

Ike

The method shown below I got from the Sun site. The method takes a byte []
and zips it, returning a byte [] of the zipped data. However, it works fine
with I set the ZipEntry's method to STORED (i.e. no compression) but not if
I set it to DEFLATED (in which case, regardless of the byte[] passed to this
method, it always returns a byte[] of length=42.). Well, the reason I want
to use this method is to compress a byte [] ! Can anyone tell me why this
would work for ZipEntry.STORED and not ZipEntry.DEFLATED ? Thanks, Ike

public static byte[] zipBytes(byte[] bytes, String aName) throws
IOException{
ByteArrayOutputStream tempOStream = null;
BufferedOutputStream tempBOStream = null;
ZipOutputStream tempZStream = null;
ZipEntry tempEntry = null;
byte[] tempBytes = null;
CRC32 tempCRC = null;
tempOStream = new ByteArrayOutputStream(bytes.length);
tempBOStream = new BufferedOutputStream(tempOStream);
tempZStream = new ZipOutputStream(tempBOStream);
tempCRC = new CRC32();
tempCRC.update(bytes, 0, bytes.length);
tempEntry = new ZipEntry(aName);
tempEntry.setMethod(ZipEntry.DEFLATED);// <--Works fine if I use
ZipEntry.STORED
tempEntry.setSize((long) bytes.length);
tempEntry.setCrc(tempCRC.getValue());
tempZStream.putNextEntry(tempEntry);
tempZStream.write(bytes, 0, bytes.length);
tempZStream.flush();
tempBytes = tempOStream.toByteArray();
tempZStream.close();
return tempBytes;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top