示例#1
0
  public static final void zip(String compressPath, String[] needCompressPaths) {
    File compressFile = new File(compressPath);

    List<File> files = Lists.newArrayList();
    for (String needCompressPath : needCompressPaths) {
      File needCompressFile = new File(needCompressPath);
      if (!needCompressFile.exists()) {
        continue;
      }
      files.add(needCompressFile);
    }
    try {
      ZipArchiveOutputStream zaos = null;
      try {
        zaos = new ZipArchiveOutputStream(compressFile);
        zaos.setUseZip64(Zip64Mode.AsNeeded);
        zaos.setEncoding("GBK");

        for (File file : files) {
          addFilesToCompression(zaos, file, "");
        }

      } catch (IOException e) {
        throw e;
      } finally {
        IOUtils.closeQuietly(zaos);
      }
    } catch (Exception e) {
      FileUtils.deleteQuietly(compressFile);
      throw new RuntimeException("压缩失败", e);
    }
  }
 /**
  * 设置压缩文件输出流
  *
  * @param zaos 压缩文件输出流
  * @param encoding 字符集
  * @param comment 注释
  */
 private static void config(ZipArchiveOutputStream zaos, String encoding, String comment) {
   zaos.setUseZip64(Zip64Mode.AsNeeded);
   if (ValidateUtilsHelper.isNotBlank(encoding)) {
     zaos.setEncoding(encoding);
   }
   if (ValidateUtilsHelper.isNotBlank(comment)) {
     zaos.setComment(comment);
   }
 }