Пример #1
0
  public List<File> unpack(File archiveFile, File targetDir) throws ArchiveException {
    // 首先判断下对应的目标文件是否存在,如存在则执行删除
    if (false == archiveFile.exists()) {
      throw new ArchiveException(String.format("[%s] not exist", archiveFile.getAbsolutePath()));
    }
    if (false == targetDir.exists() && false == NioUtils.create(targetDir, false, 3)) {
      throw new ArchiveException(
          String.format("[%s] not exist and create failed", targetDir.getAbsolutePath()));
    }

    List<File> result = new ArrayList<File>();
    ZipFile zipFile = null;
    try {
      zipFile = new ZipFile(archiveFile);
      Enumeration entries = zipFile.entries();

      while (entries.hasMoreElements()) {
        // entry
        ZipEntry entry = (ZipEntry) entries.nextElement();
        String entryName = entry.getName();
        // target
        File targetFile = new File(targetDir, entryName);
        NioUtils.create(targetFile.getParentFile(), false, 3); // 尝试创建父路径
        InputStream input = null;
        OutputStream output = null;
        try {
          output = new FileOutputStream(targetFile);
          input = zipFile.getInputStream(entry);
          NioUtils.copy(input, output);
        } finally {
          IOUtils.closeQuietly(input);
          IOUtils.closeQuietly(output);
        }
        result.add(targetFile);
      }

    } catch (Exception e) {
      throw new ArchiveException(e);
    } finally {
      if (zipFile != null) {
        try {
          zipFile.close();
        } catch (IOException ex) {
        }
      }
    }

    return result;
  }
 /**
  * Close resources.
  *
  * @throws IOException
  */
 @Override
 public void close() throws IOException {
   zip.close();
 }