当前位置: 首页 > 工具软件 > TrueZIP > 使用案例 >

使用truezip解压缩zip文件

江智
2023-12-01

 /**
  * 解压缩zip包
  *
  * @param zipfile
  *            zip包
  * @param unzippath
  *            解压缩的目標路径
  * @return
  */
 public static String unziptopath(File zipfile, String unzippath) throws AppException {
  File localfile = null;
  try {
   String xmlpath = "";
   localfile = zipfile;
   FileOutputStream fout = null; // 解压文件输出流
   byte[] buf = new byte[1024];
   ZipFile zip = new ZipFile(localfile, "GBK"); // 取出zip文件
   String zipname = localfile.getName(); // 获得zip包name
   String zipRealName =FileUtility.removeExtension(FileUtility.removePath(zipname));//zipname.substring(begin, end); // 取得zip包去掉路径和后缀名的name
   Enumeration zList = zip.entries();// 返回zip中文件的枚举对象
   while (zList.hasMoreElements()) { // 处理每个
    ZipEntry ze = (ZipEntry) zList.nextElement(); // zipentry
    if (ze.isDirectory()) {
     continue;
    }
    // zipentry的输入流
    InputStream isze = zip.getInputStream(ze);
    InputStream is = new BufferedInputStream(isze);
    // 对应zipentry的文件输出流
    fout = new FileOutputStream(getRealFileName(unzippath + "/"
      + zipRealName, ze.getName()));
    int readLen = 0;
    // 写文件
    while ((readLen = is.read(buf, 0, 1024)) != -1) {
     fout.write(buf, 0, readLen);
    }
    // 关闭输出流
    fout.close();
    is.close();
    isze.close();
    if (ze.getName().endsWith(".zip")) { // 判断是否是xmlzip
     // 如果是xmlzip包则解开xmlzip包
     xmlpath =  xmlunzip(unzippath + "/" + zipRealName + "/"
       + ze.getName(), unzippath + "/" + zipRealName);
    }
   }
   zip.close();
   return xmlpath;
  } catch (Exception e) {
   throw new AppException("文件类型不对,请确认是否为加密zip文件!");
  } finally {
   if (localfile != null) {
    try {
     FileUtility.forceDelete(localfile);
    } catch(IOException ie) {
     throw new AppException("删除文件出错", ie);
    }
   }
  }

 }

 类似资料: