import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 文件压缩工具类
*/
public class ZipCompress {
private static Logger log = LoggerFactory.getLogger(ZipCompress.class);
private String zipFileName; // 目的地Zip文件
private String sourceFileName; // 源文件(待压缩的文件或文件夹)
public ZipCompress(String zipFileName, String sourceFileName) {
this.zipFileName = zipFileName;
this.sourceFileName = sourceFileName;
}
public void zip() throws Exception {
//File zipFile = new File(zipFileName);
log.info("开始压缩中...");
long begin = System.currentTimeMillis();
//创建zip输出流
File zipFile = new File(zipFileName);
if (!zipFile.exists()) {
zipFile.createNewFile();
}
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
//创建缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(out);
File sourceFile = new File(sourceFileName);
//调用函数
compress(out, bos, sourceFile, sourceFile.getName());
bos.close();
out.close();
long currentTimeMillis = System.currentTimeMillis();
log.info(zipFileName + "压缩完成(100%)....." + (currentTimeMillis - begin) + "ms");
}
public void zipNoDirectory() throws Exception {
//File zipFile = new File(zipFileName);
log.info("开始压缩中...");
long begin = System.currentTimeMillis();
//创建zip输出流
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
//创建缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(out);
File sourceFile = new File(sourceFileName);
if (sourceFile.isDirectory()) {
File[] flist = sourceFile.listFiles();
for (int i = 0; i < flist.length; i++) {
compress(out, bos, flist[i], flist[i].getName());
}
}
//调用函数
// compress(out,bos,sourceFile,sourceFile.getName());
bos.close();
out.close();
long currentTimeMillis = System.currentTimeMillis();
log.info(zipFileName + "压缩完成(100%)....." + (currentTimeMillis - begin) + "ms");
}
public void compress(ZipOutputStream out, BufferedOutputStream bos, File sourceFile, String base) throws Exception {
//如果路径为目录(文件夹)
if (sourceFile.isDirectory()) {
//取出文件夹中的文件(或子文件夹)
File[] flist = sourceFile.listFiles();
if (flist.length == 0) {//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
//System.out.println("空:******"+base+"/");
out.putNextEntry(new ZipEntry(base + File.separator));
} else {//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
for (int i = 0; i < flist.length; i++) {
compress(out, bos, flist[i], base + "/" + flist[i].getName());
}
}
} else {//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
out.putNextEntry(new ZipEntry(base));
FileInputStream fos = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fos);
int tag;
// System.out.println(base);
//将源文件写入到zip文件中
while ((tag = bis.read()) != -1) {
bos.write(tag);
}
bos.flush();
bis.close();
fos.close();
}
}
/**
* 多个分散文件压缩
* @param srcFiles 要压缩的文件
* @param zipFile 目的地zip文件
*/
public static void zipFiles(File[] srcFiles, File zipFile) {
// 判断压缩后的文件存在不,不存在则创建
if (!zipFile.exists()) {
try {
zipFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 创建 FileOutputStream 对象
FileOutputStream fileOutputStream = null;
// 创建 ZipOutputStream
ZipOutputStream zipOutputStream = null;
// 创建 FileInputStream 对象
FileInputStream fileInputStream = null;
try {
// 实例化 FileOutputStream 对象
fileOutputStream = new FileOutputStream(zipFile);
// 实例化 ZipOutputStream 对象
zipOutputStream = new ZipOutputStream(fileOutputStream);
// 创建 ZipEntry 对象
ZipEntry zipEntry = null;
// 遍历源文件数组
for (int i = 0; i < srcFiles.length; i++) {
// 将源文件数组中的当前文件读入 FileInputStream 流中
fileInputStream = new FileInputStream(srcFiles[i]);
// 实例化 ZipEntry 对象,源文件数组中的当前文件
zipEntry = new ZipEntry(srcFiles[i].getName());
zipOutputStream.putNextEntry(zipEntry);
// 该变量记录每次真正读的字节个数
int len;
// 定义每次读取的字节数组
byte[] buffer = new byte[1024];
while ((len = fileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
}
zipOutputStream.closeEntry();
zipOutputStream.close();
fileInputStream.close();
fileOutputStream.close();
} catch (IOException e) {
log.error("zip打包失败,错误信息如下:"+e.getMessage());
}
}
}