import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
/**
* 压缩、解压文件工具类
*
* @author
*
*/
public class ZipUtil {
/**
* 默认编编码方式
*/
private static final String ENCODING = "GBK";
/**
* 默认压缩到的目录
*/
private static final String DEFALUT_DEST = "c:/dest.zip";
/**
* 压缩文件
*
* @param source
* 源路径,文件或者目录
* @param destinct
* 目标路径
* @throws IOException
*/
@SuppressWarnings("rawtypes")
public static void zip(String source, String dest) throws IOException {
List fileList = loadFilename(new File(source));
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
new File(dest)));
zos.setEncoding(ENCODING);
byte[] buffere = new byte[8192];
int length;
BufferedInputStream bis;
for (int i = 0; i < fileList.size(); i++) {
File file = (File) fileList.get(i);
zos.putNextEntry(new ZipEntry(getEntryName(source, file)));
bis = new BufferedInputStream(new FileInputStream(file));
while (true) {
length = bis.read(buffere);
if (length == -1)
break;
zos.write(buffere, 0, length);
}
bis.close();
zos.closeEntry();
}
zos.close();
}
/**
* 压缩文件到默认的指定路径下
*
* @param source
* 源路径,文件或者目录
* @throws IOException
*/
public static void zip(String source) throws IOException {
zip(source, DEFALUT_DEST);
}
/**
* 解压ZIP文件
*
* @param source
* 源路径
* @param dest
* 目标路径
* @throws IOException
*/
@SuppressWarnings("rawtypes")
public static void unZip(String source, String dest) throws IOException {
ZipFile zip = new ZipFile(source);
Enumeration en = zip.getEntries();
ZipEntry entry = null;
byte[] buffer = new byte[8192];
int length = -1;
InputStream input = null;
BufferedOutputStream bos = null;
File file = null;
while (en.hasMoreElements()) {
entry = (ZipEntry) en.nextElement();
if (entry.isDirectory()) {
continue;
}
input = zip.getInputStream(entry);
file = new File(dest, entry.getName());
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
bos = new BufferedOutputStream(new FileOutputStream(file));
while (true) {
length = input.read(buffer);
if (length == -1)
break;
bos.write(buffer, 0, length);
}
bos.close();
input.close();
}
zip.close();
}
/**
* 递归获得该文件下所有文件名(不包括目录名)
*
* @param file
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private static List loadFilename(File file) {
List filenameList = new ArrayList();
if (file.isFile()) {
filenameList.add(file);
}
if (file.isDirectory()) {
for (File f : file.listFiles()) {
filenameList.addAll(loadFilename(f));
}
}
return filenameList;
}
/**
* 获得zip entry 字符串
*
* @param base
* @param file
* @return
*/
private static String getEntryName(String base, File file) {
File baseFile = new File(base);
String filename = file.getPath();
if (baseFile.getParentFile().getParentFile() == null) {
return filename.substring(baseFile.getParent().length());
}
return filename.substring(baseFile.getParent().length() + 1);
}
}