應好友須要,整理一下Java的壓縮算法,先從ZLib開始。
相關連接:
Java壓縮技術(一) ZLib
Java壓縮技術(二) ZIP壓縮——Java原生實現
Java壓縮技術(三) ZIP解壓縮——Java原生實現
Java壓縮技術(四) GZIP——Java原生實現
Java壓縮技術(五) GZIP相關——瀏覽器解析
Java壓縮技術(六) BZIP2——Commons實現
Java壓縮技術(七) TAR——Commons實現
有關ZLib可參見官方主頁
http://www.zlib.net/
ZLib能夠簡單的理解爲壓縮/解壓縮算法,它與ZIP、RAR等歸檔算法有所不一樣,與bzip2比較接近。
壓縮工具代碼以下:
- /**
- * 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
- * 待壓縮數據
- * @return byte[] 壓縮後的數據
- */
- 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
- * 待壓縮數據
- *
- * @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
- * 待壓縮的數據
- * @return byte[] 解壓縮後的數據
- */
- 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[] 解壓縮後的數據
- */
- 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();
- }
- }
/**
* 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
* 待壓縮數據
* @return byte[] 壓縮後的數據
*/
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
* 待壓縮數據
*
* @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
* 待壓縮的數據
* @return byte[] 解壓縮後的數據
*/
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[] 解壓縮後的數據
*/
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();
}
}
測試用例代碼以下:
- /**
- * 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:zlex.dongliang@gmail.com">樑棟</a>
- * @version 1.0
- * @since 1.0
- */
- public class ZLibUtilsTest {
-
- @Test
- public final void testBytes() {
- System.err.println("字節壓縮/解壓縮測試");
- String inputStr = "snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org";
- 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 = "snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org";
- 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);
- }
- }
/**
* 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:zlex.dongliang@gmail.com">樑棟</a>
* @version 1.0
* @since 1.0
*/
public class ZLibUtilsTest {
@Test
public final void testBytes() {
System.err.println("字節壓縮/解壓縮測試");
String inputStr = "snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org";
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 = "snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org";
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);
}
}
輸入結果
- 字節壓縮/解壓縮測試
- 輸入字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
- 輸入字節長度: 59
- 壓縮後字節長度: 39
- 解壓縮後字節長度: 59
- 輸出字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
- 文件壓縮/解壓縮測試
- 輸入字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
- 輸入字節長度: 59
- 壓縮後字節長度: 39
- 解壓縮後字節長度: 59
- 輸出字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
字節壓縮/解壓縮測試
輸入字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
輸入字節長度: 59
壓縮後字節長度: 39
解壓縮後字節長度: 59
輸出字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
文件壓縮/解壓縮測試
輸入字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
輸入字節長度: 59
壓縮後字節長度: 39
解壓縮後字節長度: 59
輸出字符串: snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zlex.org
應該怎麼計算呢?原數據長度59字節,壓縮後39字節,大約是33%的壓縮率!
ZLib壓縮對大字節數據壓縮,才能反映出壓縮效果。
先佔個位兒,回頭細緻整理!