Compressing a string using Deflater

D

dave

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 ?
 
D

dave

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();
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top