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!

Create GZIP compressed data using DeflaterInputStream?

862188May 16 2011 — edited May 17 2011
want to provide to a client with an input stream that gets the content of a string in GZIP compressed format.

However, the util.zip.GZIPInputStream class can only be used to decompress, not compress data.

So I tried to use java.util.zip.DeflaterInputStream instead. However this does not seem to generate GZIP format - when I try to read the generated data back using a GZIPInputStream I get a "Not in GZIP format" error.

DeflaterInputStream can take an instance of a Deflater as its second argument and Deflater can be constructed with a second argument "nowrap" set to true. In that case, according to the docs, "if true then use GZIP compatible compression" I thought the DeflaterInputStream should generate GZIP compatible format, but I still get "Not in GZIP format".

Here is the code I use:
      InputStream is = IOUtils.toInputStream(theString, theEncoding);
      InputStream iscomp =
        new DeflaterInputStream(is,new Deflater(6,true));
     // pas iscomp to the client to read the content of theString
     // in GZIP compressed format
Is there a way to create an input stream that will provide a GZIP compressed version of theString?

GZIPOutputStream would create the correct data but I cannot use it here because it is an output stream and not an input stream as required by the client.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 14 2011
Added on May 16 2011
11 comments
1,580 views