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比較接近。
壓縮工具代碼如下:
測(cè)試用例代碼如下:
輸入結(jié)果
應(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)
應(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代碼
- /**
- * 2009-9-9
- */
- package org.zlex.commons.io;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.zip.Deflater;
- import java.util.zip.DeflaterOutputStream;
- import java.util.zip.Inflater;
- import java.util.zip.InflaterInputStream;
- /**
- * ZLib壓縮工具
- *
- * @author <a href="mailto:zlex.dongliang@gmail.com">梁棟</a>
- * @version 1.0
- * @since 1.0
- */
- public abstract class ZLibUtils {
- /**
- * 壓縮
- *
- * @param data
- * 待壓縮數(shù)據(jù)
- * @return byte[] 壓縮后的數(shù)據(jù)
- */
- public static byte[] compress(byte[] data) {
- byte[] output = new byte[0];
- Deflater compresser = new Deflater();
- compresser.reset();
- compresser.setInput(data);
- compresser.finish();
- ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
- try {
- byte[] buf = new byte[1024];
- while (!compresser.finished()) {
- int i = compresser.deflate(buf);
- bos.write(buf, 0, i);
- }
- output = bos.toByteArray();
- } catch (Exception e) {
- output = data;
- e.printStackTrace();
- } finally {
- try {
- bos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- compresser.end();
- return output;
- }
- /**
- * 壓縮
- *
- * @param data
- * 待壓縮數(shù)據(jù)
- *
- * @param os
- * 輸出流
- */
- public static void compress(byte[] data, OutputStream os) {
- DeflaterOutputStream dos = new DeflaterOutputStream(os);
- try {
- dos.write(data, 0, data.length);
- dos.finish();
- dos.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 解壓縮
- *
- * @param data
- * 待壓縮的數(shù)據(jù)
- * @return byte[] 解壓縮后的數(shù)據(jù)
- */
- public static byte[] decompress(byte[] data) {
- byte[] output = new byte[0];
- Inflater decompresser = new Inflater();
- decompresser.reset();
- decompresser.setInput(data);
- ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
- try {
- byte[] buf = new byte[1024];
- while (!decompresser.finished()) {
- int i = decompresser.inflate(buf);
- o.write(buf, 0, i);
- }
- output = o.toByteArray();
- } catch (Exception e) {
- output = data;
- e.printStackTrace();
- } finally {
- try {
- o.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- decompresser.end();
- return output;
- }
- /**
- * 解壓縮
- *
- * @param is
- * 輸入流
- * @return byte[] 解壓縮后的數(shù)據(jù)
- */
- public static byte[] decompress(InputStream is) {
- InflaterInputStream iis = new InflaterInputStream(is);
- ByteArrayOutputStream o = new ByteArrayOutputStream(1024);
- try {
- int i = 1024;
- byte[] buf = new byte[i];
- while ((i = iis.read(buf, 0, i)) > 0) {
- o.write(buf, 0, i);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return o.toByteArray();
- }
- }
測(cè)試用例代碼如下:
Java代碼
- /**
- * 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壓縮測(cè)試用例
- *
- * @author <a href="mailto:zlex.dongliang@gmail.com">梁棟</a>
- * @version 1.0
- * @since 1.0
- */
- public class ZLibUtilsTest {
- @Test
- public final void testBytes() {
- System.err.println("字節(jié)壓縮/解壓縮測(cè)試");
- String inputStr = "snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org";
- System.err.println("輸入字符串:\t" + inputStr);
- byte[] input = inputStr.getBytes();
- System.err.println("輸入字節(jié)長(zhǎng)度:\t" + input.length);
- byte[] data = ZLibUtils.compress(input);
- System.err.println("壓縮后字節(jié)長(zhǎng)度:\t" + data.length);
- byte[] output = ZLibUtils.decompress(data);
- System.err.println("解壓縮后字節(jié)長(zhǎng)度:\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("文件壓縮/解壓縮測(cè)試");
- String inputStr = "snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org";
- System.err.println("輸入字符串:\t" + inputStr);
- byte[] input = inputStr.getBytes();
- System.err.println("輸入字節(jié)長(zhǎng)度:\t" + input.length);
- try {
- FileOutputStream fos = new FileOutputStream(file);
- ZLibUtils.compress(input, fos);
- fos.close();
- System.err.println("壓縮后字節(jié)長(zhǎng)度:\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("解壓縮后字節(jié)長(zhǎng)度:\t" + output.length);
- String outputStr = new String(output);
- System.err.println("輸出字符串:\t" + outputStr);
- assertEquals(inputStr, outputStr);
- }
- }
輸入結(jié)果
Consloe代碼
- 字節(jié)壓縮/解壓縮測(cè)試
- 輸入字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
- 輸入字節(jié)長(zhǎng)度: 59
- 壓縮后字節(jié)長(zhǎng)度: 39
- 解壓縮后字節(jié)長(zhǎng)度: 59
- 輸出字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
- 文件壓縮/解壓縮測(cè)試
- 輸入字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
- 輸入字節(jié)長(zhǎng)度: 59
- 壓縮后字節(jié)長(zhǎng)度: 39
- 解壓縮后字節(jié)長(zhǎng)度: 59
- 輸出字符串: 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) 編輯 收藏