public static byte [ ] generateTarGz ( File sourceDirectory , String pathPrefix , File chaincodeMetaInf ) throws IOException { logger . trace ( format ( "generateTarGz: sourceDirectory: %s, pathPrefix: %s, chaincodeMetaInf: %s" , sourceDirectory == null ? "null" : sourceDirectory . getAbsolutePath ( ) , pathPrefix , chaincodeMetaInf == null ? "null" : chaincodeMetaInf . getAbsolutePath ( ) ) ) ; ByteArrayOutputStream bos = new ByteArrayOutputStream ( 500000 ) ; String sourcePath = sourceDirectory . getAbsolutePath ( ) ; TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream ( new GzipCompressorOutputStream ( bos ) ) ; archiveOutputStream . setLongFileMode ( TarArchiveOutputStream . LONGFILE_GNU ) ; try { Collection < File > childrenFiles = org . apache . commons . io . FileUtils . listFiles ( sourceDirectory , null , true ) ; ArchiveEntry archiveEntry ; FileInputStream fileInputStream ; for ( File childFile : childrenFiles ) { String childPath = childFile . getAbsolutePath ( ) ; String relativePath = childPath . substring ( ( sourcePath . length ( ) + 1 ) , childPath . length ( ) ) ; if ( pathPrefix != null ) { relativePath = Utils . combinePaths ( pathPrefix , relativePath ) ; } relativePath = FilenameUtils . separatorsToUnix ( relativePath ) ; if ( TRACE_ENABED ) { logger . trace ( format ( "generateTarGz: Adding '%s' entry from source '%s' to archive." , relativePath , childFile . getAbsolutePath ( ) ) ) ; } archiveEntry = new TarArchiveEntry ( childFile , relativePath ) ; fileInputStream = new FileInputStream ( childFile ) ; archiveOutputStream . putArchiveEntry ( archiveEntry ) ; try { IOUtils . copy ( fileInputStream , archiveOutputStream ) ; } finally { IOUtils . closeQuietly ( fileInputStream ) ; archiveOutputStream . closeArchiveEntry ( ) ; } } if ( null != chaincodeMetaInf ) { childrenFiles = org . apache . commons . io . FileUtils . listFiles ( chaincodeMetaInf , null , true ) ; final URI metabase = chaincodeMetaInf . toURI ( ) ; for ( File childFile : childrenFiles ) { final String relativePath = Paths . get ( "META-INF" , metabase . relativize ( childFile . toURI ( ) ) . getPath ( ) ) . toString ( ) ; if ( TRACE_ENABED ) { logger . trace ( format ( "generateTarGz: Adding '%s' entry from source '%s' to archive." , relativePath , childFile . getAbsolutePath ( ) ) ) ; } archiveEntry = new TarArchiveEntry ( childFile , relativePath ) ; fileInputStream = new FileInputStream ( childFile ) ; archiveOutputStream . putArchiveEntry ( archiveEntry ) ; try { IOUtils . copy ( fileInputStream , archiveOutputStream ) ; } finally { IOUtils . closeQuietly ( fileInputStream ) ; archiveOutputStream . closeArchiveEntry ( ) ; } } } } finally { IOUtils . closeQuietly ( archiveOutputStream ) ; } return bos . toByteArray ( ) ; }
Compress the contents of given directory using Tar and Gzip to an in - memory byte array .