private static void dirToTarArchiveOutputStreamRecursive(FileStatus dirFileStatus, FileSystem fs,
      Optional<Path> destDir, TarArchiveOutputStream tarArchiveOutputStream) throws IOException {

    Preconditions.checkState(fs.isDirectory(dirFileStatus.getPath()));

    Path dir = destDir.isPresent() ? new Path(destDir.get(), dirFileStatus.getPath().getName())
        : new Path(dirFileStatus.getPath().getName());
    dirToTarArchiveOutputStream(dir, tarArchiveOutputStream);

    for (FileStatus childFileStatus : fs.listStatus(dirFileStatus.getPath())) {
      Path childFile = new Path(dir, childFileStatus.getPath().getName());

      if (fs.isDirectory(childFileStatus.getPath())) {
        dirToTarArchiveOutputStreamRecursive(childFileStatus, fs, Optional.of(childFile), tarArchiveOutputStream);
      } else {
        try (FSDataInputStream fsDataInputStream = fs.open(childFileStatus.getPath())) {
          fileToTarArchiveOutputStream(childFileStatus, fsDataInputStream, childFile, tarArchiveOutputStream);
        }
      }
    }
  }
private static void dirToTarArchiveOutputStreamRecursive ( FileStatus dirFileStatus , FileSystem fs , Optional < Path > destDir , TarArchiveOutputStream tarArchiveOutputStream ) throws IOException { Preconditions . checkState ( fs . isDirectory ( dirFileStatus . getPath ( ) ) ) ; Path dir = destDir . isPresent ( ) ? new Path ( destDir . get ( ) , dirFileStatus . getPath ( ) . getName ( ) ) : new Path ( dirFileStatus . getPath ( ) . getName ( ) ) ; dirToTarArchiveOutputStream ( dir , tarArchiveOutputStream ) ; for ( FileStatus childFileStatus : fs . listStatus ( dirFileStatus . getPath ( ) ) ) { Path childFile = new Path ( dir , childFileStatus . getPath ( ) . getName ( ) ) ; if ( fs . isDirectory ( childFileStatus . getPath ( ) ) ) { dirToTarArchiveOutputStreamRecursive ( childFileStatus , fs , Optional . of ( childFile ) , tarArchiveOutputStream ) ; } else { try ( FSDataInputStream fsDataInputStream = fs . open ( childFileStatus . getPath ( ) ) ) { fileToTarArchiveOutputStream ( childFileStatus , fsDataInputStream , childFile , tarArchiveOutputStream ) ; } } } }
Helper method for {@link #tar(FileSystem, FileSystem, Path, Path)} that recursively adds a directory to a given
{@link TarArchiveOutputStream}.