package com.woxiaoe.study.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 文件压缩
* @author 小e
*
* 2010-5-23 下午09:24:31
*/
public class FileCompress {
public static void compress(String dest,File...files){
FileOutputStream fos = null;
CheckedOutputStream cos = null;
ZipOutputStream zos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(dest);
cos = new CheckedOutputStream(fos, new Adler32());
zos = new ZipOutputStream(cos);
bos = new BufferedOutputStream(zos);
// org.apache.tools.zip setEncoding(...)
zos.setComment("文件压缩测试");
for(File file : files){
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
zos.putNextEntry(new ZipEntry(file.getName()));
int len;
byte[] buf = new byte[1024];
while((len = bis.read(buf)) != -1){
bos.write(buf, 0, len);
}
bis.close();
bos.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(zos != null){
try {
zos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cos != null){
try {
cos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}