1. 程式人生 > >對字串進行壓縮,壓縮成gzip流,效果好不錯

對字串進行壓縮,壓縮成gzip流,效果好不錯

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以下,效果還不錯