Compressing a string using Deflater

Discussion in 'Java' started by dave@yamoo.com, May 4, 2005.

  1. Guest

    I try to compress a string, like this :

    private byte[] compressMsg(String msg)
    {
    try
    {
    byte[] input = msg.getBytes("UTF-8");
    byte[] output = new byte[input.length];

    Deflater compresser = new Deflater();
    compresser.setLevel(Deflater.DEFAULT_COMPRESSION);
    compresser.setInput(input);
    compresser.finish();
    compresser.deflate(output);

    return output;
    }
    catch (java.io.UnsupportedEncodingException uee)
    {
    uee.printStackTrace();
    return null;
    }
    }

    It seems to work, it returns a byte-array with some unreadable
    (deflated) data which transfers nicely back into the original string
    when inflated.

    But, the returned byte-array is exactly the same size as the input-array,
    so I gain no advantage from this compression (...).

    This is, of course because you have to declare the output-array before
    starting the deflation and since you don't know how much the data will
    shrink, you can only make it as big as the input.

    (I also notice that the deflated file contains mostly zero's, but this seems
    not to be 'empty' data, because if I remove these, the file will not be
    inflated back correctly).

    So my question is :
    How should this code be modified to return a byte-array with
    compressed data that is actually smaller than the original ?
     
    , May 4, 2005
    #1
    1. Advertising

  2. Guest

    On Wed, 04 May 2005 16:04:55 +0200, wrote:

    >I try to compress a string, like this :
    >
    > private byte[] compressMsg(String msg)
    > {
    > try
    > {
    > byte[] input = msg.getBytes("UTF-8");
    > byte[] output = new byte[input.length];
    >
    > Deflater compresser = new Deflater();
    > compresser.setLevel(Deflater.DEFAULT_COMPRESSION);
    > compresser.setInput(input);
    > compresser.finish();
    > compresser.deflate(output);
    >
    > return output;
    > }
    > catch (java.io.UnsupportedEncodingException uee)
    > {
    > uee.printStackTrace();
    > return null;
    > }
    > }
    >
    >It seems to work, it returns a byte-array with some unreadable
    >(deflated) data which transfers nicely back into the original string
    >when inflated.
    >
    >But, the returned byte-array is exactly the same size as the input-array,
    >so I gain no advantage from this compression (...).
    >
    >This is, of course because you have to declare the output-array before
    >starting the deflation and since you don't know how much the data will
    >shrink, you can only make it as big as the input.
    >
    >(I also notice that the deflated file contains mostly zero's, but this seems
    >not to be 'empty' data, because if I remove these, the file will not be
    >inflated back correctly).
    >
    >So my question is :
    >How should this code be modified to return a byte-array with
    >compressed data that is actually smaller than the original ?



    Never mind, I have found the solution on :
    http://javaalmanac.com/egs/java.util.zip/CompArray.html

    Quote :

    byte[] input = "some some bytes to compress".getBytes();

    // Create the compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
    int count = compressor.deflate(buf);
    bos.write(buf, 0, count);
    }
    try {
    bos.close();
    } catch (IOException e) {
    }

    // Get the compressed data
    byte[] compressedData = bos.toByteArray();
     
    , May 4, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parker Thompson

    Deflater and IBM JRE

    Parker Thompson, Jul 1, 2003, in forum: Java
    Replies:
    3
    Views:
    1,466
    Xavier Tarrago
    Jul 2, 2003
  2. NOBODY
    Replies:
    2
    Views:
    808
    Thomas Weidenfeller
    Oct 17, 2003
  3. Dr. Carsten Leue

    Deflater.setDictionary

    Dr. Carsten Leue, Jan 12, 2004, in forum: Java
    Replies:
    1
    Views:
    1,472
    Chris Uppal
    Jan 12, 2004
  4. Shea Martin
    Replies:
    2
    Views:
    377
    Shea Martin
    Aug 17, 2005
  5. Lew
    Replies:
    3
    Views:
    451
    Arne Vajhøj
    Jan 8, 2011
Loading...

Share This Page