java.util.zip.Deflater/Inflater
此类使用流行的 ZLIB 压缩程序库为通用压缩提供支持。ZLIB 压缩程序库最初是作为 PNG 图形标准的一部分开发的,不受专利的保护。有关该规范的完整描述,请参见 java.util.zip 包描述。
以下代码片段演示使用 Deflater 和 Inflater 压缩和解压缩字符串的详细过程。
// 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");
使用请参考:
http://www.gznc.edu.cn/yxsz/jjglxy/book/Java_api/java/util/zip/Deflater.html
本文介绍如何使用Java中的Deflater和Inflater类进行数据压缩与解压缩操作。通过示例代码展示将字符串转换为字节数组并进行压缩的过程,以及如何解压这些字节以还原原始数据。
1383

被折叠的 条评论
为什么被折叠?



