athrunwang

          紀(jì)元
          數(shù)據(jù)加載中……
          Java壓縮技術(shù)(一) ZLib 轉(zhuǎn)貼 Snowolf的意境空間! http://snowolf.iteye.com/blog/465433
          Snowolf的意境空間!轉(zhuǎn)貼:http://snowolf.iteye.com/blog/465433
          應(yīng)好友需要,整理一下Java的壓縮算法,先從ZLib開(kāi)始。 

          相關(guān)鏈接: 
          Java壓縮技術(shù)(一) ZLib 
          Java壓縮技術(shù)(二) ZIP壓縮——Java原生實(shí)現(xiàn) 
          Java壓縮技術(shù)(三) ZIP解壓縮——Java原生實(shí)現(xiàn) 
          Java壓縮技術(shù)(四) GZIP——Java原生實(shí)現(xiàn) 
          Java壓縮技術(shù)(五) GZIP相關(guān)——瀏覽器解析 
          Java壓縮技術(shù)(六) BZIP2——Commons實(shí)現(xiàn) 
          Java壓縮技術(shù)(七) TAR——Commons實(shí)現(xiàn) 

          有關(guān)ZLib可參見(jiàn)官方主頁(yè) http://www.zlib.net/ 
          ZLib可以簡(jiǎn)單的理解為壓縮/解壓縮算法,它與ZIP、RAR等歸檔算法有所不同,與bzip2比較接近。 

          壓縮工具代碼如下: 
          Java代碼  收藏代碼
          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:zlex.dongliang@gmail.com">梁棟</a> 
          19.  * @version 1.0 
          20.  * @since 1.0 
          21.  */  
          22. public abstract class ZLibUtils {  
          23.   
          24.     /** 
          25.      * 壓縮 
          26.      *  
          27.      * @param data 
          28.      *            待壓縮數(shù)據(jù) 
          29.      * @return byte[] 壓縮后的數(shù)據(jù) 
          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.      *            待壓縮數(shù)據(jù) 
          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.      *            待壓縮的數(shù)據(jù) 
          89.      * @return byte[] 解壓縮后的數(shù)據(jù) 
          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[] 解壓縮后的數(shù)據(jù) 
          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.     }  
          144. }  

          測(cè)試用例代碼如下: 
          Java代碼  收藏代碼
          1. /** 
          2.  * 2009-9-9 
          3.  */  
          4. package org.zlex.commons.io;  
          5.   
          6. import static org.junit.Assert.*;  
          7.   
          8. import java.io.File;  
          9. import java.io.FileInputStream;  
          10. import java.io.FileOutputStream;  
          11.   
          12. import org.junit.Test;  
          13.   
          14. /** 
          15.  * ZLib壓縮測(cè)試用例 
          16.  *  
          17.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁棟</a> 
          18.  * @version 1.0 
          19.  * @since 1.0 
          20.  */  
          21. public class ZLibUtilsTest {  
          22.   
          23.     @Test  
          24.     public final void testBytes() {  
          25.         System.err.println("字節(jié)壓縮/解壓縮測(cè)試");  
          26.         String inputStr = "snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org";  
          27.         System.err.println("輸入字符串:\t" + inputStr);  
          28.         byte[] input = inputStr.getBytes();  
          29.         System.err.println("輸入字節(jié)長(zhǎng)度:\t" + input.length);  
          30.   
          31.         byte[] data = ZLibUtils.compress(input);  
          32.         System.err.println("壓縮后字節(jié)長(zhǎng)度:\t" + data.length);  
          33.   
          34.         byte[] output = ZLibUtils.decompress(data);  
          35.         System.err.println("解壓縮后字節(jié)長(zhǎng)度:\t" + output.length);  
          36.         String outputStr = new String(output);  
          37.         System.err.println("輸出字符串:\t" + outputStr);  
          38.   
          39.         assertEquals(inputStr, outputStr);  
          40.     }  
          41.   
          42.     @Test  
          43.     public final void testFile() {  
          44.         String filename = "zlib";  
          45.         File file = new File(filename);  
          46.         System.err.println("文件壓縮/解壓縮測(cè)試");  
          47.         String inputStr = "snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org";  
          48.         System.err.println("輸入字符串:\t" + inputStr);  
          49.         byte[] input = inputStr.getBytes();  
          50.         System.err.println("輸入字節(jié)長(zhǎng)度:\t" + input.length);  
          51.   
          52.         try {  
          53.   
          54.             FileOutputStream fos = new FileOutputStream(file);  
          55.             ZLibUtils.compress(input, fos);  
          56.             fos.close();  
          57.             System.err.println("壓縮后字節(jié)長(zhǎng)度:\t" + file.length());  
          58.         } catch (Exception e) {  
          59.             fail(e.getMessage());  
          60.         }  
          61.   
          62.         byte[] output = null;  
          63.   
          64.         try {  
          65.             FileInputStream fis = new FileInputStream(file);  
          66.             output = ZLibUtils.decompress(fis);  
          67.             fis.close();  
          68.   
          69.         } catch (Exception e) {  
          70.             fail(e.getMessage());  
          71.         }  
          72.         System.err.println("解壓縮后字節(jié)長(zhǎng)度:\t" + output.length);  
          73.         String outputStr = new String(output);  
          74.         System.err.println("輸出字符串:\t" + outputStr);  
          75.   
          76.         assertEquals(inputStr, outputStr);  
          77.     }  
          78. }  

          輸入結(jié)果 
          Consloe代碼  收藏代碼
          1. 字節(jié)壓縮/解壓縮測(cè)試  
          2. 輸入字符串:  snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org  
          3. 輸入字節(jié)長(zhǎng)度: 59  
          4. 壓縮后字節(jié)長(zhǎng)度:    39  
          5. 解壓縮后字節(jié)長(zhǎng)度:   59  
          6. 輸出字符串:  snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org  
          7. 文件壓縮/解壓縮測(cè)試  
          8. 輸入字符串:  snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org  
          9. 輸入字節(jié)長(zhǎng)度: 59  
          10. 壓縮后字節(jié)長(zhǎng)度:    39  
          11. 解壓縮后字節(jié)長(zhǎng)度:   59  
          12. 輸出字符串:  snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org  


          應(yīng)該怎么計(jì)算呢?原數(shù)據(jù)長(zhǎng)度59字節(jié),壓縮后39字節(jié),大約是33%的壓縮率! 

          ZLib壓縮對(duì)大字節(jié)數(shù)據(jù)壓縮,才能反映出壓縮效果。 
          先占個(gè)位兒,回頭細(xì)致整理! 

          相關(guān)鏈接: 
          Java壓縮技術(shù)(一) ZLib 
          Java壓縮技術(shù)(二) ZIP壓縮——Java原生實(shí)現(xiàn) 
          Java壓縮技術(shù)(三) ZIP解壓縮——Java原生實(shí)現(xiàn) 
          Java壓縮技術(shù)(四) GZIP——Java原生實(shí)現(xiàn) 
          Java壓縮技術(shù)(五) GZIP相關(guān)——瀏覽器解析 
          Java壓縮技術(shù)(六) BZIP2——Commons實(shí)現(xiàn) 
          Java壓縮技術(shù)(七) TAR——Commons實(shí)現(xiàn)

          posted on 2011-12-26 09:50 AthrunWang 閱讀(585) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 磐石市| 翁牛特旗| 西乌珠穆沁旗| 和政县| 五原县| 中江县| 任丘市| 福建省| 延吉市| 乌苏市| 邢台市| 齐齐哈尔市| 筠连县| 武定县| 海口市| 方山县| 盈江县| 麻城市| 广东省| 延川县| 韶关市| 方山县| 堆龙德庆县| 鄯善县| 巫山县| 奈曼旗| 漾濞| 博爱县| 屏东县| 霍山县| 嵩明县| 分宜县| 申扎县| 九台市| 德江县| 尼木县| 英吉沙县| 互助| 皮山县| 棋牌| 区。|