ZipFile解压文件

	/**第一个参数是压缩文件路径,第二个参数是要解压的文件夹路径,文件夹可以不存在会自动生成*/
	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的解压文件方法。




                
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值