container/src/main/java/com/streamsets/datacollector/cluster/TarFileCreator.java
private static void tarFolder(String parent, String path, TarOutputStream out) throws IOException {
    BufferedInputStream src = null;
    File f = new File(path);
    String files[] = f.list();
    // is file
    if (files == null) {
      files = new String[1];
      files[0] = f.getName();
    }
    parent = ((parent == null) ? (f.isFile()) ? "" : f.getName() + "/" : parent + f.getName() + "/");
    for (int i = 0; i < files.length; i++) {
      File fe = f;
      if (f.isDirectory()) {
        fe = new File(f, files[i]);
      }
      if (fe.isDirectory()) {
        String[] fl = fe.list();
        if (fl != null && fl.length != 0) {
          tarFolder(parent, fe.getPath(), out);
        } else {
          TarEntry entry = new TarEntry(fe, parent + files[i] + "/");
          out.putNextEntry(entry);
        }
        continue;
      }
      FileInputStream fi = new FileInputStream(fe);
      src = new BufferedInputStream(fi);
      TarEntry entry = new TarEntry(fe, parent + files[i]);
      out.putNextEntry(entry);
      IOUtils.copy(src, out);
      src.close();
      out.flush();
    }
  }
private static void tarFolder(String parent, String path, TarOutputStream out) throws IOException {
    BufferedInputStream src = null;
    File f = new File(path);
    String files[] = f.list();
    // is file
    if (files == null) {
      files = new String[1];
      files[0] = f.getName();
    }
    parent = ((parent == null) ? (f.isFile()) ? "" : f.getName() + "/" : parent + f.getName() + "/");
    for (int i = 0; i < files.length; i++) {
      File fe = f;
      if (f.isDirectory()) {
        fe = new File(f, files[i]);
      }
      if (fe.isDirectory()) {
        String[] fl = fe.list();
        if (fl != null && fl.length != 0) {
          tarFolder(parent, fe.getPath(), out);
        } else {
          TarEntry entry = new TarEntry(fe, parent + files[i] + "/");
          out.putNextEntry(entry);
        }
        continue;
      }
      FileInputStream fi = new FileInputStream(fe);
      src = new BufferedInputStream(fi);
      TarEntry entry = new TarEntry(fe, parent + files[i]);
      out.putNextEntry(entry);
      IOUtils.copy(src, out);
      src.close();
      out.flush();
    }
  }
private static void tarFolder ( String parent , String path , TarOutputStream out ) throws IOException { BufferedInputStream src = null ; File f = new File ( path ) ; String files [ ] = f . list ( ) ; // is file if ( files == null ) { files = new String [ 1 ] ; files [ 0 ] = f . getName ( ) ; } parent = ( ( parent == null ) ? ( f . isFile ( ) ) ? "" : f . getName ( ) + "/" : parent + f . getName ( ) + "/" ) ; for ( int i = 0 ; i < files . length ; i ++ ) { File fe = f ; if ( f . isDirectory ( ) ) { fe = new File ( f , files [ i ] ) ; } if ( fe . isDirectory ( ) ) { String [ ] fl = fe . list ( ) ; if ( fl != null && fl . length != 0 ) { tarFolder ( parent , fe . getPath ( ) , out ) ; } else { TarEntry entry = new TarEntry ( fe , parent + files [ i ] + "/" ) ; out . putNextEntry ( entry ) ; } continue ; } FileInputStream fi = new FileInputStream ( fe ) ; src = new BufferedInputStream ( fi ) ; TarEntry entry = new TarEntry ( fe , parent + files [ i ] ) ; out . putNextEntry ( entry ) ; IOUtils . copy ( src , out ) ; src . close ( ) ; out . flush ( ) ; } }
Copied from https://raw.githubusercontent.com/kamranzafar/jtar/master/src/test/java/org/kamranzafar/jtar/JTarTest.java
Copied from https : // raw . githubusercontent . com / kamranzafar / jtar / master / src / test / java / org / kamranzafar / jtar / JTarTest . java