最近项目中有一个功能需要将文件压缩和解压,我这边最开始使用的是JDK中的类库,在网上找了一些样例,或多或少总是存在一些BUG,而且中文路径乱码的问题始终不能解决。没办法最终只有换方案,最后使用了apache-ant中的工具类来实现的,并且测试通过。源码也是从网上找到的,做了少量修改,这里给大家分享一下。

packagecom.aeai.zip;
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.Enumeration;
importjava.util.zip.CRC32;
importjava.util.zip.CheckedOutputStream;
importjava.util.zip.Deflater;
importjava.util.zip.ZipException;
importorg.apache.tools.zip.ZipEntry;
importorg.apache.tools.zip.ZipFile;
importorg.apache.tools.zip.ZipOutputStream;
/**
*利用apache提供的ant.jar,提供对单个文件与目录的压缩,并支持是否需要创建压缩源目录、中文路径
*
*@Title:
*@Description:ZipCompress
*@Version1.2
*/
publicclassZipCompress {
privatestaticbooleanisCreateSrcDir=true;//是否创建源目录
/**
*对文件夹或者文件进行压缩
*
*@Time2012-3-9上午09:32:35 create
*@paramsrc
*文件路径(全)
*@paramarchive
*压缩路径(全)
*@paramcomment
*压缩包注释
*@throwsFileNotFoundException
*@throwsIOException
*/
publicstaticvoidwriteByApacheZipOutputStream(Stringsrc, String archive,
String comment)throwsFileNotFoundException,IOException {
// ----压缩文件:
FileOutputStream f =newFileOutputStream(archive);
//使用指定校验和创建输出流
CheckedOutputStream csum =newCheckedOutputStream(f,newCRC32());
ZipOutputStream zos =newZipOutputStream(csum);
//支持中文
zos.setEncoding("GBK");
BufferedOutputStream out =newBufferedOutputStream(zos);
//设置压缩包注释
zos.setComment(comment);
//启用压缩
zos.setMethod(ZipOutputStream.DEFLATED);
//压缩级别为最强压缩,但时间要花得多一点
zos.setLevel(Deflater.BEST_COMPRESSION);
File srcFile =newFile(src);
if(!srcFile.exists()
|| (srcFile.isDirectory()&& srcFile.list().length== 0)) {
thrownewFileNotFoundException(
"File must exist and  ZIP filemust have at least one entry.");
}
//获取压缩源所在父目录
src = src.replaceAll("\\\\","/");
String prefixDir =null;
if(srcFile.isFile()) {
prefixDir = src.substring(0,src.lastIndexOf("/") + 1);
}else{
prefixDir = (src.replaceAll("/$","") +"/");
}
//如果不是根目录
if(prefixDir.indexOf("/") != (prefixDir.length() - 1)
&&isCreateSrcDir){
prefixDir = prefixDir.replaceAll("[^/]+/$","");
}
//开始压缩
writeRecursive(zos, out, srcFile,prefixDir);
out.close();
//注:校验和要在流关闭后才准备,一定要放在流被关闭后使用
System.out.println("Checksum: "+csum.getChecksum().getValue());
}
/**
*使用org.apache.tools.zip.ZipFile解压文件,它与java类库中的java.util.zip.ZipFile
*使用方式是一新的,只不过多了设置编码方式的接口。
*
*注,apache没有提供ZipInputStream类,所以只能使用它提供的ZipFile来读取压缩
文件。
*
*@paramarchive
*压缩包路径
*@paramdecompressDir
*解压路径
*@throwsIOException
*@throwsFileNotFoundException
*@throwsZipException
*/
publicstaticvoidreadByApacheZipFile(Stringarchive, String decompressDir)
throwsIOException, FileNotFoundException,ZipException {
BufferedInputStream bi;
ZipFile zf =newZipFile(archive,"GBK");//支持中文
Enumeration> e =zf.getEntries();
while(e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry)e.nextElement();
String entryName = ze2.getName();
String path = decompressDir +"/"+ entryName;
if(ze2.isDirectory()) {
System.out.println("正在创建解压目录- "+ entryName);
File decompressDirFile =newFile(path);
if(!decompressDirFile.exists()) {
decompressDirFile.mkdirs();
}
}else{
System.out.println("正在创建解压文件- "+ entryName);
String fileDir =path.substring(0, path.lastIndexOf("/"));
File fileDirFile =newFile(fileDir);
if(!fileDirFile.exists()) {
fileDirFile.mkdirs();
}
BufferedOutputStream bos =newBufferedOutputStream(
newFileOutputStream(decompressDir +"/"+ entryName));
bi =newBufferedInputStream(zf.getInputStream(ze2));
byte[] readContent =newbyte[1024];
intreadCount = bi.read(readContent);
while(readCount != -1) {
bos.write(readContent, 0,readCount);
readCount =bi.read(readContent);
}
bos.close();
}
}
zf.close();
}
/**
*递归压缩
*
*使用org.apache.tools.zip.ZipOutputStream类进行压缩,它的好处就是支持中文路径,而Java类库中的
* java.util.zip.ZipOutputStream压缩中文文件名时压缩包会出现乱码。使用apache中的这个类与java
*类库中的用法是一新的,只是能设置编码方式了。
*
*@paramzos
*@parambo
*@paramsrcFile
*@paramprefixDir
*@throwsIOException
*@throwsFileNotFoundException
*/
privatestaticvoidwriteRecursive(ZipOutputStream zos
,
BufferedOutputStream bo, FilesrcFile, String prefixDir)
throwsIOException, FileNotFoundException {
ZipEntry zipEntry;
String filePath =srcFile.getAbsolutePath().replaceAll("\\\\","/")
.replaceAll("//","/");
if(srcFile.isDirectory()) {
filePath = filePath.replaceAll("/$","") +"/";
}
String entryName =filePath.replace(prefixDir,"").replaceAll("/$","");
if(srcFile.isDirectory()) {
if(!"".equals(entryName)) {
System.out.println("正在创建目录- "+ srcFile.getAbsolutePath()
+" entryName="+ entryName);
//如果是目录,则需要在写目录后面加上/
zipEntry =newZipEntry(entryName +"/");
zos.putNextEntry(zipEntry);
}
File srcFiles[] =srcFile.listFiles();
for(inti = 0; i < srcFiles.length; i++) {
writeRecursive(zos, bo,srcFiles[i], prefixDir);
}
}else{
System.out.println("正在写文件- "+ srcFile.getAbsolutePath()
+" entryName="+ entryName);
BufferedInputStream bi =newBufferedInputStream(
newFileInputStream(srcFile));
//开始写入新的ZIP文件条目并将流定位到条目数据的开始处
zipEntry =newZipEntry(entryName);
zos.putNextEntry(zipEntry);
byte[] buffer =newbyte[1024];
intreadCount = bi.read(buffer);
while(readCount != -1) {
bo.write(buffer, 0, readCount);
readCount = bi.read(buffer);
}
//注,在使用缓冲流写压缩文件时,一个条件完后一定要刷新一把,不
//然可能有的内容就会存入到后面条目中去了
bo.flush();
//文件读完后关闭
bi.close();
}
}
/**
*@paramargs
*@throwsIOException
*/
publicstaticvoidmain(String[] args)throwsIOException {
//writeByApacheZipOutputStream("D:/data2", "D:/data2.zip","");
readByApacheZipFile("D:/data2.zip","D:/data3");
}
}

JAVA中文件压缩、解压好方法分享文档      下载