Query about compression and decompression in Java using java.util.zip package

Query about compression and decompression in Java using java.util.zip package

Post by Matt Humph » Thu, 25 Nov 2004 00:43:42


"yogesh" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...

The answer lies in how you think about streams. Rather than thinking about
them as being only containers that hold data (e.g. I write the objects to
the stream and then compress the stream), think about them as
transformations. An object output stream accepts objects as input and
writes them out as bytes. A GZIP stream accepts bytes as input and writes
out a compressed equivalent, etc. With that understanding, you see that the
problem is that your GZIP stream is not receiving any data (you never write
to it), and the compression header it has to write out is written out to an
object stream, not a data stream.

The original layering works because your program (at the highest level) is
writing objects to an object stream. It should largely not care that the
object stream is then compressed or not or written to a socket or whatever.
At the next level down the individual bytes that comprise the object stream
are compressed and written out as bytes to the bottom layer file stream that
puts them on disk. To read the data you simply reverse the process: file
stream to decompressor to object stream.

Cheers,
Matt Humphrey XXXX@XXXXX.COM http://www.iviz.com/