package com.accp.commons.util;
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.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.lang.StringUtils;
/**
* 压缩解压缩工具包
* @author yoyoflash
* @date:2012-6-15 下午12:03:27
*/
public class ZipTools {
private static final int ZIP_BUFFER = 2048;
private int zip_level;
public ZipTools() {
zip_level = 0;
}
/**
* setLevel level - the compression level (0-9)
*
* @param level
* int
*/
public void setLevel(int level) {
this.zip_level = level;
}
/**
* 支持对于文件和目录的压缩
*
* @param inputFile
* File 要压缩的目录或者文件
* @param outputFile
* File 输出的压缩文件
* @throws ZipException
*/
public void zipFile(File inputFile, File outputFile) throws ZipException {
try {
BufferedOutputStream bout = new BufferedOutputStream(
new FileOutputStream(outputFile), ZIP_BUFFER);
ZipOutputStream out = new ZipOutputStream(bout);
zip(out, inputFile, inputFile.getName());
out.close();
} catch (IOException ex) {
throw new ZipException(ex.getMessage());
}
}
/**
* 批量压缩功能
*
* @param inputFiles
* File[] 要压缩的文件数组
* @param outputFile
* File 压缩后的文件
* @throws ZipException
*/
public void zipFiles(File[] inputFiles, File outputFile)
throws ZipException {
try {
BufferedOutputStream bout = new BufferedOutputStream(
new FileOutputStream(outputFile), ZIP_BUFFER);
ZipOutputStream out = new ZipOutputStream(bout);
for (int i = 0; i < inputFiles.length; i++) {
zip(out, inputFiles[i], inputFiles[i].getName());
}
out.close();
} catch (IOException ex) {
throw new ZipException(ex.getMessage());
}
}
/**
* 解压缩文件
*
* @param inputFile
* File 原压缩文件
* @param outputFile
* File 解压目录
* @throws ZipException
*/
public void unZipFile(File inputFile, File outputFile) throws ZipException {
try {
FileInputStream tin = new FileInputStream(inputFile);
CheckedInputStream cin = new CheckedInputStream(tin, new CRC32());
BufferedInputStream bufferIn = new BufferedInputStream(cin,
ZIP_BUFFER);
ZipInputStream in = new ZipInputStream(bufferIn);
ZipEntry z = in.getNextEntry();
while (z != null) {
String name = z.getName();
if (z.isDirectory()) {
File f = new File(outputFile.getPath() + File.separator
+ name);
f.mkdirs();
} else {
File f = new File(outputFile.getPath() + File.separator
+ name);
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
byte data[] = new byte[ZIP_BUFFER];
int b;
while ((b = in.read(data, 0, ZIP_BUFFER)) != -1) {
out.write(data, 0, b);
}
out.close();
}
z = in.getNextEntry();
}
in.close();
}
catch (IOException ex) {
throw new ZipException(ex.getMessage());
}
}
private void zip(ZipOutputStream out, File f, String base)
throws FileNotFoundException, ZipException {
if (zip_level != 0) {
out.setLevel(zip_level);
}
if (f.isDirectory()) {
zipDirectory(out, f, base);
} else {
if (StringUtils.isEmpty(base)) {
base = f.getName();
}
zipLeapFile(out, f, base);
}
}
private void zipDirectory(ZipOutputStream out, File f, String base)
throws FileNotFoundException, ZipException {
File[] files = f.listFiles();
if (zip_level != 0) {
out.setLevel(zip_level);
}
try {
out.putNextEntry(new ZipEntry(base + "/"));
} catch (IOException ex) {
throw new ZipException(ex.getMessage());
}
if (StringUtils.isEmpty(base)) {
base = new String();
} else {
base = base + "/";
}
for (int i = 0; i < files.length; i++) {
zip(out, files[i], base + files[i].getName());
}
}
private void zipLeapFile(ZipOutputStream out, File f, String base)
throws FileNotFoundException, ZipException {
if (zip_level != 0) {
out.setLevel(zip_level);
}
try {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
BufferedInputStream bufferIn = new BufferedInputStream(in,
ZIP_BUFFER);
byte[] data = new byte[ZIP_BUFFER];
int b;
while ((b = bufferIn.read(data, 0, ZIP_BUFFER)) != -1) {
out.write(data, 0, b);
}
bufferIn.close();
} catch (IOException ex) {
throw new ZipException(ex.getMessage());
}
}
}