private List < File > unTar ( final File inputFile , final File outputDir ) throws FileNotFoundException , IOException , ArchiveException { _log . info ( String . format ( "Untaring %s to dir %s." , inputFile . getAbsolutePath ( ) , outputDir . getAbsolutePath ( ) ) ) ; final List < File > untaredFiles = new LinkedList < File > ( ) ; final InputStream is = new FileInputStream ( inputFile ) ; final TarArchiveInputStream debInputStream = ( TarArchiveInputStream ) new ArchiveStreamFactory ( ) . createArchiveInputStream ( "tar" , is ) ; TarArchiveEntry entry = null ; while ( ( entry = ( TarArchiveEntry ) debInputStream . getNextEntry ( ) ) != null ) { final File outputFile = new File ( outputDir , entry . getName ( ) ) ; if ( entry . isDirectory ( ) ) { _log . info ( String . format ( "Attempting to write output directory %s." , outputFile . getAbsolutePath ( ) ) ) ; if ( ! outputFile . exists ( ) ) { _log . info ( String . format ( "Attempting to create output directory %s." , outputFile . getAbsolutePath ( ) ) ) ; if ( ! outputFile . mkdirs ( ) ) { throw new IllegalStateException ( String . format ( "Couldn't create directory %s." , outputFile . getAbsolutePath ( ) ) ) ; } } } else { _log . info ( String . format ( "Creating output file %s." , outputFile . getAbsolutePath ( ) ) ) ; final OutputStream outputFileStream = new FileOutputStream ( outputFile ) ; IOUtils . copy ( debInputStream , outputFileStream ) ; outputFileStream . close ( ) ; } untaredFiles . add ( outputFile ) ; } debInputStream . close ( ) ; return untaredFiles ; }
Untar an input file into an output file .