http://snowolf.iteye.com/blog/465433

轉自http://snowolf.iteye.com/blog/465433


壓縮代碼如下


  1. /** 

  2.  * 2009-9-9 

  3.  */  

  4. package org.zlex.commons.io;  

  5.   

  6. import java.io.ByteArrayOutputStream;  


  7. import java.io.IOException;  

  8. import java.io.InputStream;  

  9. import java.io.OutputStream;  

  10. import java.util.zip.Deflater;  

  11. import java.util.zip.DeflaterOutputStream;  

  12. import java.util.zip.Inflater;  

  13. import java.util.zip.InflaterInputStream;  

  14.   

  15. /** 

  16.  * ZLib壓縮工具 

  17.  *  

  18.  * @author <a href="mailto:[email protected]">樑棟</a> 

  19.  * @version 1.0 

  20.  * @since 1.0 

  21.  */  

  22. public abstract class ZLibUtils {  

  23.   

  24.     /** 

  25.      * 壓縮 

  26.      *  

  27.      * @param data 

  28.      *            待壓縮數據 

  29.      * @return byte[] 壓縮後的數據 

  30.      */  

  31.     public static byte[] compress(byte[] data) {  

  32.         byte[] output = new byte[0];  

  33.   

  34.         Deflater compresser = new Deflater();  

  35.   

  36.         compresser.reset();  

  37.         compresser.setInput(data);  

  38.         compresser.finish();  

  39.         ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);  

  40.         try {  

  41.             byte[] buf = new byte[1024];  

  42.             while (!compresser.finished()) {  

  43.                 int i = compresser.deflate(buf);  

  44.                 bos.write(buf, 0, i);  

  45.             }  

  46.             output = bos.toByteArray();  

  47.         } catch (Exception e) {  

  48.             output = data;  

  49.             e.printStackTrace();  

  50.         } finally {  

  51.             try {  

  52.                 bos.close();  

  53.             } catch (IOException e) {  

  54.                 e.printStackTrace();  

  55.             }  

  56.         }  

  57.         compresser.end();  

  58.         return output;  

  59.     }  

  60.   

  61.     /** 

  62.      * 壓縮 

  63.      *  

  64.      * @param data 

  65.      *            待壓縮數據 

  66.      *  

  67.      * @param os 

  68.      *            輸出流 

  69.      */  

  70.     public static void compress(byte[] data, OutputStream os) {  

  71.         DeflaterOutputStream dos = new DeflaterOutputStream(os);  

  72.   

  73.         try {  

  74.             dos.write(data, 0, data.length);  

  75.   

  76.             dos.finish();  

  77.   

  78.             dos.flush();  

  79.         } catch (IOException e) {  

  80.             e.printStackTrace();  

  81.         }  

  82.     }  

  83.   

  84.     /** 

  85.      * 解壓縮 

  86.      *  

  87.      * @param data 

  88.      *            待壓縮的數據 

  89.      * @return byte[] 解壓縮後的數據 

  90.      */  

  91.     public static byte[] decompress(byte[] data) {  

  92.         byte[] output = new byte[0];  

  93.   

  94.         Inflater decompresser = new Inflater();  

  95.         decompresser.reset();  

  96.         decompresser.setInput(data);  

  97.   

  98.         ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);  

  99.         try {  

  100.             byte[] buf = new byte[1024];  

  101.             while (!decompresser.finished()) {  

  102.                 int i = decompresser.inflate(buf);  

  103.                 o.write(buf, 0, i);  

  104.             }  

  105.             output = o.toByteArray();  

  106.         } catch (Exception e) {  

  107.             output = data;  

  108.             e.printStackTrace();  

  109.         } finally {  

  110.             try {  

  111.                 o.close();  

  112.             } catch (IOException e) {  

  113.                 e.printStackTrace();  

  114.             }  

  115.         }  

  116.   

  117.         decompresser.end();  

  118.         return output;  

  119.     }  

  120.   

  121.     /** 

  122.      * 解壓縮 

  123.      *  

  124.      * @param is 

  125.      *            輸入流 

  126.      * @return byte[] 解壓縮後的數據 

  127.      */  

  128.     public static byte[] decompress(InputStream is) {  

  129.         InflaterInputStream iis = new InflaterInputStream(is);  

  130.         ByteArrayOutputStream o = new ByteArrayOutputStream(1024);  

  131.         try {  

  132.             int i = 1024;  

  133.             byte[] buf = new byte[i];  

  134.   

  135.             while ((i = iis.read(buf, 0, i)) > 0) {  

  136.                 o.write(buf, 0, i);  

  137.             }  

  138.   

  139.         } catch (IOException e) {  

  140.             e.printStackTrace();  

  141.         }  

  142.         return o.toByteArray();  

  143.     }  


測試用例代碼如下:
/** 
 * 2009-9-9 
 */  
package org.zlex.commons.io;  
  
import static org.junit.Assert.*;  
  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
  
import org.junit.Test;  
  
/** 
 * ZLib壓縮測試用例 
 *  
 * @author <a href="mailto:[email protected]">樑棟</a> 
 * @version 1.0 
 * @since 1.0 
 */  
public class ZLibUtilsTest {  
  
    @Test  
    public final void testBytes() {  
        System.err.println("字節壓縮/解壓縮測試");  
        String inputStr = "[email protected];[email protected];[email protected]";  
        System.err.println("輸入字符串:\t" + inputStr);  
        byte[] input = inputStr.getBytes();  
        System.err.println("輸入字節長度:\t" + input.length);  
  
        byte[] data = ZLibUtils.compress(input);  
        System.err.println("壓縮後字節長度:\t" + data.length);  
  
        byte[] output = ZLibUtils.decompress(data);  
        System.err.println("解壓縮後字節長度:\t" + output.length);  
        String outputStr = new String(output);  
        System.err.println("輸出字符串:\t" + outputStr);  
  
        assertEquals(inputStr, outputStr);  
    }  
  
    @Test  
    public final void testFile() {  
        String filename = "zlib";  
        File file = new File(filename);  
        System.err.println("文件壓縮/解壓縮測試");  
        String inputStr = "[email protected];[email protected];[email protected]";  
        System.err.println("輸入字符串:\t" + inputStr);  
        byte[] input = inputStr.getBytes();  
        System.err.println("輸入字節長度:\t" + input.length);  
  
        try {  
  
            FileOutputStream fos = new FileOutputStream(file);  
            ZLibUtils.compress(input, fos);  
            fos.close();  
            System.err.println("壓縮後字節長度:\t" + file.length());  
        } catch (Exception e) {  
            fail(e.getMessage());  
        }  
  
        byte[] output = null;  
  
        try {  
            FileInputStream fis = new FileInputStream(file);  
            output = ZLibUtils.decompress(fis);  
            fis.close();  
  
        } catch (Exception e) {  
            fail(e.getMessage());  
        }  
        System.err.println("解壓縮後字節長度:\t" + output.length);  
        String outputStr = new String(output);  
        System.err.println("輸出字符串:\t" + outputStr);  
  
        assertEquals(inputStr, outputStr);  
    }  
}  

輸入結果
字節壓縮/解壓縮測試  
輸入字符串:  [email protected];[email protected];[email protected]  
輸入字節長度: 59  
壓縮後字節長度:    39  
解壓縮後字節長度:   59  
輸出字符串:  [email protected];[email protected];[email protected]  
文件壓縮/解壓縮測試  
輸入字符串:  [email protected];[email protected];[email protected]  
輸入字節長度: 59  
壓縮後字節長度:    39  
解壓縮後字節長度:   59  
輸出字符串:  [email protected];[email protected];[email protected]  

應該怎麼計算呢?原數據長度59字節,壓縮後39字節,大約是33%的壓縮率!

ZLib壓縮對大字節數據壓縮,才能反映出壓縮效果。 
先佔個位兒,回頭細緻整理!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章