public static void writeTarGz ( Path dirPath , OutputStream output ) throws IOException , InterruptedException { GzipCompressorOutputStream zipStream = new GzipCompressorOutputStream ( output ) ; TarArchiveOutputStream archiveStream = new TarArchiveOutputStream ( zipStream ) ; for ( Path subPath : Files . walk ( dirPath ) . collect ( toList ( ) ) ) { if ( Thread . interrupted ( ) ) { throw new InterruptedException ( ) ; } File file = subPath . toFile ( ) ; TarArchiveEntry entry = new TarArchiveEntry ( file , dirPath . relativize ( subPath ) . toString ( ) ) ; archiveStream . putArchiveEntry ( entry ) ; if ( file . isFile ( ) ) { try ( InputStream fileIn = Files . newInputStream ( subPath ) ) { IOUtils . copy ( fileIn , archiveStream ) ; } } archiveStream . closeArchiveEntry ( ) ; } archiveStream . finish ( ) ; zipStream . finish ( ) ; }
Creates a gzipped tar archive from the given path streaming the data to the give output stream .