Example #1
0
  private static void append(
      File file,
      FileFilter filter,
      int baseNameLen,
      String addedTopFolder,
      TarArchiveOutputStream tarOut)
      throws IOException {

    String name = file.getAbsolutePath();
    if (name.length() <= baseNameLen) name = "";
    else name = name.substring(baseNameLen);
    if (File.separatorChar == '\\') name = name.replace('\\', '/');
    if (addedTopFolder != null) name = addedTopFolder + '/' + name;

    if (FileUtils.isSymlink(file)) {
      String linkTarget = FileUtils.readSymbolicLink(file);
      if (linkTarget != null) {
        TarArchiveEntry entry = new TarArchiveEntry(name, TarConstants.LF_SYMLINK);
        entry.setName(name);
        entry.setLinkName(linkTarget);
        tarOut.putArchiveEntry(entry);
      }
      return;
    }

    ArchiveEntry entry = tarOut.createArchiveEntry(file, name);
    tarOut.putArchiveEntry(entry);
    File[] children = file.listFiles(filter);
    if (children != null) {
      tarOut.closeArchiveEntry();
      // This is a directory. Append its children
      for (File child : children) append(child, filter, baseNameLen, addedTopFolder, tarOut);
      return;
    }

    // Append the content of the file
    InputStream input = new FileInputStream(file);
    try {
      StreamUtil.copy(input, tarOut);
      tarOut.closeArchiveEntry();
    } finally {
      StreamUtil.close(input);
    }
  }