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

  • 本文关键字:压缩 结果 数据 解压缩 java
  • 更新时间 :
  • 英文 :


下面提到的是我制作的一个演示程序,用于验证Deflator和Inflator类的功能。

我的主要方法中的源代码:-

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,85,115,101,114,50,86,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,85,115,101,114,50,86,97,108,117,0,0,0]

正如您在上面看到的,被压缩的数据和对压缩数据进行膨胀后生成的数据是不相同的。请帮忙。

如果您尝试使用:

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

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

最新更新