/**第一个参数是压缩文件路径,第二个参数是要解压的文件夹路径,文件夹可以不存在会自动生成*/
public void ZipDecompress(String frompath,String topath) throws IOException
{
ZipFile zf=new ZipFile(new File(frompath));
InputStream inputStream;
Enumeration en=zf.getEntries();
while(en.hasMoreElements())
{
ZipEntry zn=(ZipEntry) en.nextElement();
if(!zn.isDirectory())
{
inputStream=zf.getInputStream(zn);
File f=new File(topath+zn.getName());
File file=f.getParentFile();
file.mkdirs();
System.out.println(zn.getName()+"---"+zn.getSize());
FileOutputStream outputStream=new FileOutputStream(topath+zn.getName());
int len=0;
byte bufer[]=new byte[1024];
while(-1!=(len=inputStream.read(bufer)))
{
outputStream.write(bufer, 0, len);
}
outputStream.close();
}
}
}
注意:只能解压Zip格式压缩文件,rar的不行。里面的ZipFile与ZipEntry 并不是用的JAVA提供的JAR包中的类,JAVA中提供的这两个类不支持中文,如果你的压缩文件里没中文,可以用JAVA提供的类,只需要把Enumeration en=zf.getEntries();改为Enumeration en=zf.entries();即可。
如果想用这段代码解压缩带中文的Zip文件,下载ant.jar包吧!路径:下载地址
解压文件除了ZipFile,ZipInputStream应该也能做到,不过ant.jar包里没这个类,只提供了ZipFile,而java提供的JAVA包里的那个类又有中文字符问题,现在我还没试成功ZipInputStream的解压文件方法。