我在公司项目过程需要用到zip文件的压缩与解压的功能,于是自己研究了一下,找了一些,自己写了一个压缩和解压缩的代码,支持中文,可以文件嵌套(注意其中所用的类是ant.jar中的包中的类,我用的是1.6.0版本)

/***//**
*用于对指定源文件路径下的所有文件压缩到指定的文件中
*@paramdesString需要压缩成的文件名
*@paramsrcString压缩文件源路径,可以是文件或文件夹
*@returnboolean如果压缩成功返回true,否则表示失败
*/

publicstaticbooleanFile2Zip(Stringdes,Stringsrc)...{

booleansuccess=true;//压缩成功标志
Filesrcpath=newFile(src);
ZipOutputStreamout=null;

try...{
out=newZipOutputStream(newFileOutputStream(newFile(
des)));//创建压缩输出流
out.setEncoding("gbk");
compress(srcpath,out,"");//开始压缩
}

catch(FileNotFoundExceptionex)...{
success=false;
}

catch(IOExceptionex1)...{
success=false;
}

finally...{

if(out!=null)...{

try...{
out.close();
}

catch(IOExceptionex2)...{
}
}
}
returnsuccess;
}


/***//**
*
*@paramsrcFile
*@paramoutZipOutputStream
*@parambaseString
*@throwsIOException
*/
publicstaticvoidcompress(Filesrc,ZipOutputStreamout,Stringbase)throws

IOException...{

if(src.isDirectory())...{//如果是目录
File[]files=src.listFiles();//列举出目录中所有的内容
out.putNextEntry(newZipEntry(base+"/"));//放入一级目录
base=base.length()==0?"":base+"/";

for(inti=0;i<files.length;i++)...{
compress(files[i],out,base+files[i].getName());
}

}

else...{//如果是文件

if("".equals(base))...{
out.putNextEntry(newZipEntry(base+"/"));
base=src.getName();
}
out.putNextEntry(newZipEntry(base));
FileInputStreamin=newFileInputStream(src);
byte[]data=newbyte[4096];
intb;

while((b=in.read(data))!=-1)...{
out.write(data,0,b);
}
in.close();

}
}

/***//**
*解压缩文件
*@paramsrcStringzip所在路径c:/test/kk.zip
*@paramdesString希望存放的目录
*@throwsIOException
*@throwsIOException
*/
publicstaticvoiddecompress(Stringsrc,Stringdes)throwsIOException

...{
ZipFilefile=null;
FileOutputStreamfout=null;
DataOutputStreamdout=null;

try...{
file=newZipFile(src);
InputStreamin=null;
des=des.replace('/','/');

if(des.startsWith("//"))...{
des=des.replaceFirst("//","//");
}
Enumerationen=file.getEntries();

while(en.hasMoreElements())...{
ZipEntryentry=(ZipEntry)en.nextElement();

if(entry.isDirectory())...{//文件夹
Filedirectory=newFile(des+"/"+entry.getName());
if(!directory.exists())
directory.mkdirs();

}else...{//文件
Stringpath=entry.getName();
path=path.replace('/','/');

if(path.startsWith("//"))...{
path=path.replaceFirst("//","//");
}
intpos=path.lastIndexOf('/');

if(pos!=-1)...{
path=path.substring(0,pos+1);
Filed=newFile(des+"/"+path);
if(!d.exists())
d.mkdirs();
}

try...{
Filefiles=newFile(entry.getName());
Filef=newFile(des+"/"+files.getPath());

fout=newFileOutputStream(f);
dout=newDataOutputStream(fout);
in=file.getInputStream(entry);

byte[]b=newbyte[4096];
intlen=0;

while((len=in.read(b))!=-1)...{
dout.write(b,0,len);
}


}catch(IOExceptione)...{
throwe;

}finally...{
if(fout!=null)
fout.close();
if(dout!=null)
dout.close();
if(in!=null)
in.close();
}
}
}

}catch(IOExceptione)...{
throwe;

}finally...{
if(file!=null)
file.close();
if(fout!=null)
fout.close();
if(dout!=null)
dout.close();
}
}

/***//**
*解压缩zip文件
*@paramsrcString
*@paramdesString
*@returnboolean成功返回true;
*/

publicstaticbooleanzipDecompress(Stringsrc,Stringdes)...{
booleansuccess=true;

try...{
decompress(src,des);
}

catch(FileNotFoundExceptionex)...{
ex.printStackTrace();
success=false;
}

catch(IOExceptionex)...{
ex.printStackTrace();
success=false;
}

returnsuccess;
}
该类有很多不完善的地方,各位可以提意见,共同交流,谢谢