Java 解压文件

 

public void testReadZip() throws Exception {
  // InputStream is=new BufferedInputStream(new FileInputStream());
  String baseDir = "C://Documents and Settings//ws//桌面//新建文件夹";
  ZipFile zfile = new ZipFile("C://Documents and Settings//ws//桌面//新建文件夹//dd.zip");
  System.out.println(zfile.getName());
  Enumeration zList = zfile.entries();
  ZipEntry ze = null;
  byte[] buf = new byte[1024];
  while (zList.hasMoreElements()) {
   // 从ZipFile中得到一个ZipEntry
   ze = (ZipEntry) zList.nextElement();
   if (ze.isDirectory()) {
    System.out.println("Dir: " + ze.getName() + " skipped..");
    continue;
   }
   System.out.println("Extracting: " + ze.getName() + "/t"
     + ze.getSize() + "/t" + ze.getCompressedSize());
   // 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
   OutputStream os = new BufferedOutputStream(new FileOutputStream(
     getRealFileName(baseDir, ze.getName())));
   InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
   int readLen = 0;
   while ((readLen = is.read(buf, 0, 1024)) != -1) {
    os.write(buf, 0, readLen);
   }
   is.close();
   os.close();
   System.out.println("Extracted: " + ze.getName());
  }
  zfile.close();
}


private File getRealFileName(String baseDir, String absFileName) {//指定根目录// 相对路径名——ZipEntry中的name
  String[] dirs = absFileName.split("/");
  // System.out.println(dirs.length);
  File ret = new File(baseDir);
  if (dirs.length > 1) {
   for (int i = 0; i < dirs.length - 1; i++) {
    ret = new File(ret, dirs[i]);
   }
  }
  if (!ret.exists()) {
   ret.mkdirs();
  }
  ret = new File(ret, dirs[dirs.length - 1]);
  return ret;
}  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值