JZlib的用法 ( by quqi99 )
作者:张华 发表于:2007-07-30 ( http://blog.csdn.net/quqi99 )
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明。
JZlib是Zlib的JAVA版本,Zlib用于压缩。压缩分九级(-1为默认压缩比 9为最高压缩比 0为不压缩 1为快速压缩)。本例子缺点是:
一、不能进行目录压缩。
二、读取文件时没有经过缓存。当待压缩的文件很大时会报错。
package example;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.jcraft.jzlib.*;
/**缺点:不能按目录压缩。
* @author 张华
* @time 2007-7-30
* @description reference http://tianxiagod.spaces.live.com/
*/
public class TestJZlib {
//压缩的文件长度,压缩、解压时均要用,挺关键。
//要确保方法compressfile()与uncompressfile()参数一致
static int resLen = 0;
/**
* 压缩
*
* @param data
* @param type
* 压缩方法为一个整数 -1为默认压缩比 9为最高压缩比 0为不压缩 1为快速压缩
* @return
*/
public static byte[] compressfile(byte[] data, int type,int len) {
int err;
int comprLen = len;
byte[] compr = new byte[comprLen];
ZStream c_stream = new ZStream();
err = c_stream.deflateInit(type);
CHECK_ERR(c_stream, err, "deflateInit");
c_stream.next_in = data;
c_stream.next_in_index = 0;
c_stream.next_out = compr;
c_stream.next_out_index = 0;
while (c_stream.total_in != data.length
&& c_stream.total_out < comprLen) {
c_stream.avail_in = c_stream.avail_out = 1; // 置初值
err = c_stream.deflate(JZlib.Z_NO_FLUSH);
CHECK_ERR(c_stream, err, "deflate");
}
System.out.println("压缩前--" + c_stream.total_in + "字节");
while (true) {
c_stream.avail_out = 1;
err = c_stream.deflate(JZlib.Z_FINISH);
if (err == JZlib.Z_STREAM_END) {
break;
}
CHECK_ERR(c_stream, err, "deflate");
}
System.out.println("压缩后--" + c_stream.total_out + "字节");
err = c_stream.deflateEnd();
CHECK_ERR(c_stream, err, "deflateEnd");
byte[] zipfile = new byte[(int) c_stream.total_out];
System.arraycopy(compr, 0, zipfile, 0, zipfile.length);
return zipfile;
}
public static byte[] uncompressfile(byte[] data,int len) {
int err;
int uncomprLen = len;
byte[] uncompr = new byte[uncomprLen];
ZStream d_stream = new ZStream();
err = d_stream.inflateInit();
CHECK_ERR(d_stream, err, "inflateInit");
d_stream.next_in = data;
d_stream.next_in_index = 0;
d_stream.next_out = uncompr;
d_stream.next_out_index = 0;
while (d_stream.total_out < uncomprLen
&& d_stream.total_in < uncomprLen) {
d_stream.avail_in = d_stream.avail_out = 1;
err = d_stream.inflate(JZlib.Z_NO_FLUSH);
if (err == JZlib.Z_STREAM_END) {
break;
}
CHECK_ERR(d_stream, err, "inflate");
}
System.out.println("解压缩前--" + d_stream.total_in + "字节");
System.out.println("解压缩后--" + d_stream.total_out + "字节");
err = d_stream.inflateEnd();
CHECK_ERR(d_stream, err, "inflateEnd");
byte[] unzipfile = new byte[(int) d_stream.total_out];
System.arraycopy(uncompr, 0, unzipfile, 0, unzipfile.length);
return unzipfile;
}
static void CHECK_ERR(ZStream z, int err, String msg) {
if (err != JZlib.Z_OK) {
if (z.msg != null) {
System.out.print(z.msg + " ");
}
System.out.println(msg + " error: " + err);
System.exit(1);
}
}
static void zip(File input, File output, int compressFactor) {
if (!input.exists())
return;
if (!output.getParentFile().exists())
output.getParentFile().mkdir();
try {
FileInputStream in = new FileInputStream(input);
FileOutputStream out = new FileOutputStream(output);
resLen = in.available();
byte[] buff = new byte[resLen];
in.read(buff);
byte[] suBuf = compressfile(buff, compressFactor,resLen);
out.write(suBuf, 0, suBuf.length); // 写压缩文件
in.close();
out.close();
System.out.println("压缩完毕!" + input.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
static void unZip(File input, File output) {
if (!input.exists())
return;
if (!output.getParentFile().exists())
output.getParentFile().mkdir();
try {
FileInputStream in = new FileInputStream(input);
FileOutputStream out = new FileOutputStream(output);
byte[] buff = new byte[resLen];
in.read(buff);
byte[] suBuff = uncompressfile(buff,resLen);
out.write(suBuff, 0, suBuff.length); // 写压缩文件
in.close();
out.close();
System.out.println("解压完毕!" + input.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
TestJZlib test = new TestJZlib();
//压缩
File input = new File("f://搜索引擎原理系统与设计.pdf");
File output = new File("f://test.bz2");
test.zip(input, output, 9);
//解压
File output2 = new File("f://test.jpg");
test.unZip(output, output2);
}
}