コード例 #1
0
ファイル: ZipUtil.java プロジェクト: lxmhope/jodd
  /** Adds byte content into the zip as a file. */
  public static void addToZip(ZipOutputStream zos, byte[] content, String path, String comment)
      throws IOException {
    while (path.length() != 0 && path.charAt(0) == '/') {
      path = path.substring(1);
    }

    if (StringUtil.endsWithChar(path, '/')) {
      path = path.substring(0, path.length() - 1);
    }

    ZipEntry zipEntry = new ZipEntry(path);
    zipEntry.setTime(System.currentTimeMillis());

    if (comment != null) {
      zipEntry.setComment(comment);
    }

    zos.putNextEntry(zipEntry);

    InputStream is = new ByteArrayInputStream(content);
    try {
      StreamUtil.copy(is, zos);
    } finally {
      StreamUtil.close(is);
    }

    zos.closeEntry();
  }
コード例 #2
0
ファイル: ZipUtil.java プロジェクト: lxmhope/jodd
  public static void addFolderToZip(ZipOutputStream zos, String path, String comment)
      throws IOException {
    while (path.length() != 0 && path.charAt(0) == '/') {
      path = path.substring(1);
    }

    // add folder record
    if (!StringUtil.endsWithChar(path, '/')) {
      path += '/';
    }

    ZipEntry zipEntry = new ZipEntry(path);
    zipEntry.setTime(System.currentTimeMillis());

    if (comment != null) {
      zipEntry.setComment(comment);
    }

    zipEntry.setSize(0);
    zipEntry.setCrc(0);

    zos.putNextEntry(zipEntry);
    zos.closeEntry();
  }
コード例 #3
0
ファイル: ZipUtil.java プロジェクト: lxmhope/jodd
  /**
   * Adds single entry to ZIP output stream.
   *
   * @param zos zip output stream
   * @param file file or folder to add
   * @param path relative path of file entry; if <code>null</code> files name will be used instead
   * @param comment optional comment
   * @param recursive when set to <code>true</code> content of added folders will be added, too
   */
  public static void addToZip(
      ZipOutputStream zos, File file, String path, String comment, boolean recursive)
      throws IOException {
    if (file.exists() == false) {
      throw new FileNotFoundException(file.toString());
    }

    if (path == null) {
      path = file.getName();
    }

    while (path.length() != 0 && path.charAt(0) == '/') {
      path = path.substring(1);
    }

    boolean isDir = file.isDirectory();

    if (isDir) {
      // add folder record
      if (!StringUtil.endsWithChar(path, '/')) {
        path += '/';
      }
    }

    ZipEntry zipEntry = new ZipEntry(path);
    zipEntry.setTime(file.lastModified());

    if (comment != null) {
      zipEntry.setComment(comment);
    }

    if (isDir) {
      zipEntry.setSize(0);
      zipEntry.setCrc(0);
    }

    zos.putNextEntry(zipEntry);

    if (!isDir) {
      InputStream is = new FileInputStream(file);
      try {
        StreamUtil.copy(is, zos);
      } finally {
        StreamUtil.close(is);
      }
    }

    zos.closeEntry();

    // continue adding

    if (recursive && file.isDirectory()) {
      boolean noRelativePath = StringUtil.isEmpty(path);

      final File[] children = file.listFiles();

      if (children != null && children.length != 0) {
        for (File child : children) {
          String childRelativePath = (noRelativePath ? StringPool.EMPTY : path) + child.getName();
          addToZip(zos, child, childRelativePath, comment, recursive);
        }
      }
    }
  }