/**
* ClassName: ZipHelper.java
* Created on 2013
* Copyrights 2013 hi.csdn.net/tjcyjd All rights reserved.
* site: http://hi.csdn.net/tjcyjd
* email: 908599713@qq.com
*/
package com.kinder.common;
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.ArrayList;
import java.util.List;
import java.util.zip.ZipInputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* 对指定文件或目录进行压缩和解压缩的工具类
*
* @author yjd
*/
public class ZipHelper {
private ZipHelper() {
}
/**
* 把指定源文件压缩到目标文件中,使用的是zip格式
*
* @param src
* 要压缩的文件
* @param dest
* 压缩后生成的文件(zip格式)
*/
public static void zipFile(File src, File dest) throws RuntimeException {
BufferedInputStream bis = null;
ZipOutputStream zos = null;
byte[] b = new byte[8192];
try {
bis = new BufferedInputStream(new FileInputStream(src));// 针对源文件创建带缓冲的字节输入流来读数据
zos = new ZipOutputStream(new FileOutputStream(dest)); // 针对目标文件创建压缩输出流
// 一个被压缩文件就是压缩文件中的一个条目
zos.putNextEntry(new ZipEntry(src.getName())); // 开始写入新的 ZIP
// 文件条目并将流定位到条目数据的开始处
for (int len = 0; (len = bis.read(b)) != -1;) {
zos.write(b, 0, len);
}
zos.flush(); // 刷新输出流
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOHelper.close(bis, zos);
}
}
/**
* 把指定目录下的所有子孙目录和文件全部压缩到指定目标文件(zip格式)中 ,目标文件名为"源目录名.zip"
*
* @param srcDir
* @param destBasePath
*/
public static void zipDir(File srcDir, File destBasePath) {
zipDir(srcDir, destBasePath, null);
}
/**
* 把指定目录下的所有子孙目录和文件全部压缩到指定目标文件(zip格式)中
*
* @param srcDir
* 要压缩的目录
* @param destBasePath
* 存放zip文件的目录
* @param destFileName
* 目标文件的名。如果没有提供,目标文件的名为"源目录名.zip"
*/
public static void zipDir(File srcDir, File destBasePath,
String destFileName) throws RuntimeException {
if (!srcDir.exists() || !srcDir.isDirectory()) {
return;
}
byte[] b = new byte[8192];
ZipOutputStream zos = null;
BufferedInputStream bis = null;
try {
if (null == destFileName || "".equals(destFileName)) {
destFileName = srcDir.getName() + ".zip";
}
zos = new ZipOutputStream(new FileOutputStream(new File(
destBasePath, destFileName)));
List
subFiles = new ArrayList();
getAllSubFiles(srcDir, subFiles);
if (subFiles.size() > 0) { // 有子文件
for (File file : subFiles) { // 压缩每个子文件
bis = new BufferedInputStream(new FileInputStream(file));
// 往zip中添加一个条目
String zipEntryName = file.getAbsolutePath();
// 去掉指定目录的父目录路径名
File parent = srcDir.getParentFile(); // 得到指定目录的父目录
if (parent != null) {
String parentName = parent.getAbsolutePath();
zipEntryName = zipEntryName.substring(zipEntryName
.indexOf(parentName)
+ parentName.length() + 1);
}
zos.putNextEntry(new ZipEntry(zipEntryName));
for (int len = 0; (len = bis.read(b)) != -1;) {
zos.write(b, 0, len);
}
zos.flush();
bis.close();
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeExcept