package test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class testZip {
// 压缩
public static byte[] compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes("UTF-8"));
gzip.close();
return out.toByteArray();
}
// 解压缩
public static byte[] uncompress(byte[] str) throws IOException {
if (str == null || str.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(str);
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
return out.toByteArray();
}
public static void main(String[] args) throws IOException {
StringBuffer bf = new StringBuffer();
bf.append("234235423sdfgsatg43qr4rfsetuyw45t3wfeszdfvm 0394tivq0m234rfqa2,-r0kaw03 5jhtqca9203rjm0,qva9tj0qa3wj445");
String data= bf.toString();
System.out.println("压缩前内容:" + data);
System.out.println("压缩前大小:" + data.length());
String outdata = new String(testZip.compress(data));
System.out.println("压缩后内容:" + outdata);
System.out.println("压缩后大小:" + outdata.length());
String undata = null;
undata = new String(testZip.uncompress(outdata.getBytes("UTF-8")));
System.out.println("解缩后内容:" + undata);
System.out.println("解缩后大小:" + undata.length());
}
}
压缩只有就得到byte[]的字节流,数据大小对重复数据大的要小很多,我测试时使用了2000个MD5值,从6K压缩到1以下,效果还不错