user3279837
user3279837

Reputation: 65

First compressing and than decompressing the same compressed data gives different results

Below mentioned is a demo program that i made to verify functionality of Deflator and Inflator classes.

Source code inside my main method:-

byte[] decom = {49, 52, 49, 53, 56, 52, 56, 50, 53, 54, 55, 54, 53, 0, 50, 0,   
    52, 0, 79, 98, 106, 101, 99, 116, 78, 97, 109, 101, 50, 0, 85, 115, 101, 114,
    50, 0, 86, 97, 108, 117, 101, 52, 0};

byte[] compressedData = new byte[decom.length];
Deflater compressor = new Deflater();
compressor.setInput(decom);
System.out.println("Decompressed data : " + Arrays.toString(decom));    
compressor.finish();

int sizeofCompressedData = compressor.deflate(compressedData);    
System.out.println("Compressed data : " + Arrays.toString(compressedData));

Inflater inflater = new Inflater();
byte[] decompressed = new byte[decom.length];
inflater.setInput(compressedData, 0, sizeofCompressedData);

try
{
    inflater.inflate(decompressed); // resultLength
}
catch (DataFormatException e)
{
    decompressed = null;
}

System.out.println("Compressed data decompressed again: "
    + Arrays.toString(decompressed));

After compiling and running it I am getting following output:-

Decompressed data : [49, 52, 49, 53, 56, 52, 56, 50, 53, 54, 55, 54, 53, 0, 50, 0, 52, 0, 79, 98, 106, 101, 99, 116, 78, 97, 109, 101, 50, 0, 85, 115, 101, 114, 50, 0, 86, 97, 108, 117, 101, 52, 0]

Compressed data : [120, -100, 51, 52, 49, 52, -75, 48, -79, 48, 50, 53, 51, 55, 51, 101, 48, 98, 48, 97, -16, 79, -54, 74, 77, 46, -15, 75, -52, 77, 53, 98, 8, 45, 78, 45, 50, 98, 8, 75, -52, 41, 77]

Compressed data decompressed again: [49, 52, 49, 53, 56, 52, 56, 50, 53, 54, 55, 54, 53, 0, 50, 0, 52, 0, 79, 98, 106, 101, 99, 116, 78, 97, 109, 101, 50, 0, 85, 115, 101, 114, 50, 0, 86, 97, 108, 117, 0, 0, 0]

As you can see above that the data that was compressed and the data that was generated after inflating the compressed data are not same.Please help.

Upvotes: 1

Views: 64

Answers (1)

Nick Wilson
Nick Wilson

Reputation: 4989

If you try it with:

byte[] compressedData = new byte[ decom.length + 2 ];

...it works. It looks like your compressed data is taking more space than your decompressed data.

Upvotes: 1

Related Questions