提供同样的解压方法,使用流行的ZLIB压缩库。ZLIb压缩库最初作为png图形标准开发的,没有受到专利的限制。


下面代码演示如何使用inflater和deflater。

 // Encode a String into bytes
 String inputString = "blahblahblah??";
 byte[] input = inputString.getBytes("UTF-8");

 // Compress the bytes
 byte[] output = new byte[100];
 Deflater compresser = new Deflater();
 compresser.setInput(input);
 compresser.finish();
 int compressedDataLength = compresser.deflate(output);

 // Decompress the bytes
 Inflater decompresser = new Inflater();
 decompresser.setInput(output, 0, compressedDataLength);
 byte[] result = new byte[100];
 int resultLength = decompresser.inflate(result);
 decompresser.end();

// Decode the bytes into a String
 String outputString = new String(result, 0, resultLength, "UTF-8");


构造体:
1。public Inflater(boolean nowrap)
如果nowrap为true,那么ZLIB的头和校验和字段将不会被使用。这样就提供了压缩格式的兼容性,可以被Gzip和pkzip使用。

注意:使用nowrap选项,它也是必要的提交额外的dummy字节作为输入。zlib本地库为了提供某种优化也会加入某些dummy字节,

public Inflater()默认是关闭nowrap选项的。

当然,在应用中,为了兼容压缩,不用检查头和校验和,通常是打开该选项。

评论
发表评论

您还没有登录,请登录后发表评论

Azi
搜索本博客
存档
最新评论