public static void tar(Path inputPath, Path outputPath, boolean gZipped, boolean childrenOnly) throws IOException {
        if (!Files.exists(inputPath)) {
            throw new FileNotFoundException("File not found " + inputPath);
        }
        FileUtils.touch(outputPath.toFile());

        try (TarArchiveOutputStream tarArchiveOutputStream = buildTarStream(outputPath, gZipped)) {
            if (!Files.isDirectory(inputPath)) {
                TarArchiveEntry tarEntry = new TarArchiveEntry(inputPath.getFileName().toString());
                if (inputPath.toFile().canExecute()) {
                    tarEntry.setMode(tarEntry.getMode() | 0755);
                }
                putTarEntry(tarArchiveOutputStream, tarEntry, inputPath);
            } else {
                Path sourcePath = inputPath;
                if (!childrenOnly) {
                    // In order to have the dossier as the root entry
                    sourcePath = inputPath.getParent();
                }
                Files.walkFileTree(inputPath, EnumSet.of(FOLLOW_LINKS), Integer.MAX_VALUE,
                        new TarDirWalker(sourcePath, tarArchiveOutputStream));
            }
            tarArchiveOutputStream.flush();
        }
    }
public static void tar ( Path inputPath , Path outputPath , boolean gZipped , boolean childrenOnly ) throws IOException { if ( ! Files . exists ( inputPath ) ) { throw new FileNotFoundException ( "File not found " + inputPath ) ; } FileUtils . touch ( outputPath . toFile ( ) ) ; try ( TarArchiveOutputStream tarArchiveOutputStream = buildTarStream ( outputPath , gZipped ) ) { if ( ! Files . isDirectory ( inputPath ) ) { TarArchiveEntry tarEntry = new TarArchiveEntry ( inputPath . getFileName ( ) . toString ( ) ) ; if ( inputPath . toFile ( ) . canExecute ( ) ) { tarEntry . setMode ( tarEntry . getMode ( ) | 0755 ) ; } putTarEntry ( tarArchiveOutputStream , tarEntry , inputPath ) ; } else { Path sourcePath = inputPath ; if ( ! childrenOnly ) { sourcePath = inputPath . getParent ( ) ; } Files . walkFileTree ( inputPath , EnumSet . of ( FOLLOW_LINKS ) , Integer . MAX_VALUE , new TarDirWalker ( sourcePath , tarArchiveOutputStream ) ) ; } tarArchiveOutputStream . flush ( ) ; } }
Recursively tar file

@param inputPath
file path can be directory
@param outputPath
where to put the archived file
@param childrenOnly
if inputPath is directory and if childrenOnly is true, the archive will contain all of its children, else the archive
contains unique entry which is the inputPath itself
@param gZipped
compress with gzip algorithm