Java对Zip文件的支持不是很强大,有一些需要自己实现的代码,我在网上找了很多代码,都不能用于生产,要不就是流没有被关闭,要不就是Exception处理很随意,下面是我修改过并测试过的代码能用于生产的压缩和解压缩的代码,做一个代码备份。
使用递归算法将一个文件夹压缩成Zip文件:
static final int BUFFER = 8192;
public static void compress(String srcPath , String dstPath) throws IOException{
File srcFile = new File(srcPath);
File dstFile = new File(dstPath);
if (!srcFile.exists()) {
throw new FileNotFoundException(srcPath + "不存在!");
}
FileOutputStream out = null;
ZipOutputStream zipOut = null;
try {
out = new FileOutputStream(dstFile);
CheckedOutputStream cos = new CheckedOutputStream(out,new CRC32());
zipOut = new ZipOutputStream(cos);
String baseDir = "";
compress(srcFile, zipOut, baseDir);
}
finally {
if(null != zipOut){
zipOut.close();
out = null;
}
if(null != out){
out.close();
}
}
}
private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException{
if (file.isDirectory()) {
compressDirectory(file, zipOut, baseDir);
} else {
compressFile(file, zipOut, baseDir);
}
}
/** 压缩一个目录 */
private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) throws IOException{
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
compress(files[i], zipOut, baseDir + dir.getName() + "/");
}
}
/** 压缩一个文件 */
private static void compressFile(File file, ZipOutputStream zipOut, String baseDir) throws IOException{
if (!file.exists()){
return;
}
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(baseDir + file.getName());
zipOut.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
zipOut.write(data, 0, count);
}
}finally {
if(null != bis){
bis.close();
}
}
}
将Zip文件加压缩出来,包含所有文件和文件夹到目标目录:
public static void decompress(String zipFile , String dstPath)throws IOException{
File pathFile = new File(dstPath);
if(!pathFile.exists()){
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(zipFile);
for(Enumeration entries = zip.entries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = null;
OutputStream out = null;
try{
in = zip.getInputStream(entry);
String outPath = (dstPath+"/"+zipEntryName).replaceAll("\\*", "/");;
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
continue;
}
out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
}
finally {
if(null != in){
in.close();
}
if(null != out){
out.close();
}
}
}
zip.close();
}
调用示例:
public static void main(String[] args)throws Exception{
String targetFolderPath = "/Users/fred/zipFile/zipFolder";
String rawZipFilePath = "/Users/fred/zipFile/raw.zip";
String newZipFilePath = "/Users/fred/zipFile/new.zip";
//将Zip文件解压缩到目标目录
CompressUtil.decompress(rawZipFilePath , targetFolderPath);
//将目标目录的文件压缩成Zip文件
CompressUtil.compress(targetFolderPath , newZipFilePath);
}