利用Java.util.zip包 解压压缩文件
最近我们一个项目组做人才招聘项目,里面有文件压缩的功能,好奇之下自己也弄了一个
测试了一下,就做了一个压缩的测试代码如下:
package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class TestZip {
static final int BUFFER = 2048;

public static void main(String argv[]) {
zipFiles("c:\\myfigs.zip", "c:\\test");
}

private static void zipFiles(String outZipFilePath, String filesPath) {
try {
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(outZipFilePath);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from filesPath
File f = new File(filesPath);
String files[] = f.list();
for (int i = 0; i < files.length; i++) {
System.out.println(
"Adding: " + files<i>);
FileInputStream fi = new FileInputStream(filesPath
+ File.separator + files<i>);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(files<i>);
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}


参考资料:http://www.360doc.com/showWeb/0/0/208147.aspx
lunzi   2008-06-13 11:30:39 评论:3   阅读:2007   引用:0
tango @2011-01-22 16:53:43  tango
不支持递归压缩呀
无题 @2008-10-08 18:04:49  lunzi
把尖括号换成中括号。
无题 @2008-10-08 15:01:31  
files<i>什么意思?

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog