java的解压压缩文件的程序

  1. public static void unzipFile(File zip, File dir) throws IOException {
  2.         ZipFile zipFile = new ZipFile(zip.getPath());
  3.         Enumeration e = zipFile.getEntries();
  4.         ZipEntry zipEntry = null;
  5.         List folderList = new ArrayList();
  6.         List fileList = new ArrayList();
  7.         while (e.hasMoreElements())
  8.         {
  9.             zipEntry = (ZipEntry) e.nextElement();
  10.             if (zipEntry.isDirectory())
  11.             {
  12.                 folderList.add(zipEntry);
  13.             }
  14.             else if (!zipEntry.isDirectory())
  15.             {
  16.                 fileList.add(zipEntry);
  17.             }
  18.         }
  19.         for (int i = 0; i < folderList.size(); i++)
  20.         {
  21.             zipEntry = (ZipEntry) folderList.get(i);
  22.             String name = zipEntry.getName();
  23.             File f = new File(dir, name);
  24.             name = name.substring(0, name.length() - 1);
  25.             f.mkdirs();
  26.         }
  27.         for (int j = 0; j < fileList.size(); j++)
  28.         {
  29.             zipEntry = (ZipEntry) fileList.get(j);
  30.             String name = zipEntry.getName();
  31.             File f = new File(dir, name);
  32.             if (!f.exists())
  33.             {
  34.                 f.createNewFile();
  35.                 InputStream in = zipFile.getInputStream(zipEntry);
  36.                 FileOutputStream out = new FileOutputStream(f);
  37.                 int c;
  38.                 byte[] by = new byte[1024];
  39.                 while ((c = in.read(by)) != -1)
  40.                 {
  41.                     out.write(by, 0, c);
  42.                 }
  43.                 out.close();
  44.                 in.close();
  45.             }
  46.         }
  47.         zipFile.close();
  48.     } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值