首先压缩和解压缩相同的压缩数据会得到不同的结果

时间:2014-03-04 13:16:12

标签: java

下面提到的是我用来验证Deflator和Inflator类功能的演示程序。

我的main方法中的源代码: -

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

编译并运行后,我得到以下输出: -

解压缩数据:[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,086,97,108,117,101,52,0]

压缩数据:[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 ]

压缩数据再次解压缩:[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,086,97,108,117,0,0,0]

正如您在上面所看到的那样,压缩的数据和膨胀压缩数据后生成的数据不一样。请帮助。

1 个答案:

答案 0 :(得分:1)

如果您尝试使用:

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

......它有效。看起来您的压缩数据比解压缩数据占用的空间更多。