ZIP包解压缩

一个ZIP压缩包解压的代码,使用了ANT.JAR包解决了解压过程中的中文问题

由于解压后,中文目录名的前一半是用ISO5586-1编码的  后一半是用UTF-8的

所以用一半的编码转换难以实现

java 代码
  1. import java.io.BufferedInputStream;   
  2. import java.io.BufferedOutputStream;   
  3. import java.io.File;   
  4. import java.io.FileOutputStream;   
  5. import java.io.InputStream;   
  6. import org.apache.tools.zip.ZipEntry;   
  7. import org.apache.tools.zip.ZipFile;   
  8. public class FileUnit {   
  9. public void unzipFile(String zipFileName) throws Exception {   
  10.         try {   
  11.                 File f = new File(zipFileName);   
  12.                 ZipFile zipFile = new ZipFile(zipFileName);   
  13.                 if((!f.exists()) && (f.length() <= 0)) {   
  14.                         throw new Exception("要解压的文件不存在!");   
  15.                 }   
  16.                 String strPath, gbkPath, strtemp;   
  17.                 File tempFile = new File(f.getParent());   
  18.                 strPath = tempFile.getAbsolutePath();   
  19.                 java.util.Enumeration e = zipFile.getEntries();   
  20.                 while(e.hasMoreElements()) {   
  21.                         org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();   
  22.                         gbkPath=zipEnt.getName();   
  23.                         if(zipEnt.isDirectory()) {   
  24.                                 strtemp = strPath + "/" + gbkPath;   
  25.                                 File dir = new File(strtemp);   
  26.                                 dir.mkdirs();   
  27.                                 continue;   
  28.                         } else {   
  29.                                 //读写文件   
  30.                                 InputStream is = zipFile.getInputStream(zipEnt);   
  31.                                 BufferedInputStream bis = new BufferedInputStream(is);   
  32.                                 gbkPath=zipEnt.getName();   
  33.                                 strtemp = strPath + "/" + gbkPath;   
  34.   
  35.                                 //建目录   
  36.                                 String strsubdir = gbkPath;   
  37.                                 for(int i = 0; i < strsubdir.length(); i++) {   
  38.                                         if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {   
  39.                                                 String temp = strPath + "/"  
  40.                                                         + strsubdir.substring(0, i);   
  41.                                                 File subdir = new File(temp);   
  42.                                                 if(!subdir.exists())   
  43.                                                         subdir.mkdir();   
  44.                                         }   
  45.                                 }   
  46.   
  47.                                 FileOutputStream fos = new FileOutputStream(strtemp);   
  48.                                 BufferedOutputStream bos = new BufferedOutputStream(fos);   
  49.                                 int c;   
  50.                                 while((c = bis.read()) != -1) {   
  51.                                         bos.write((byte) c);   
  52.                                 }   
  53.                                 bos.close();   
  54.                                 fos.close();   
  55.                         }   
  56.                 }   
  57.         } catch(Exception e) {   
  58.                 e.printStackTrace();   
  59.                 throw e;   
  60.         }   
  61.     }   
  62. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值