public static void unTar ( File inFile , File untarDir ) throws IOException { if ( ! untarDir . mkdirs ( ) ) { if ( ! untarDir . isDirectory ( ) ) { throw new IOException ( "Mkdirs failed to create " + untarDir ) ; } } StringBuffer untarCommand = new StringBuffer ( ) ; boolean gzipped = inFile . toString ( ) . endsWith ( "gz" ) ; if ( gzipped ) { untarCommand . append ( " gzip -dc '" ) ; untarCommand . append ( FileUtil . makeShellPath ( inFile ) ) ; untarCommand . append ( "' | (" ) ; } untarCommand . append ( "cd '" ) ; untarCommand . append ( FileUtil . makeShellPath ( untarDir ) ) ; untarCommand . append ( "' ; " ) ; untarCommand . append ( "tar -xf " ) ; if ( gzipped ) { untarCommand . append ( " -)" ) ; } else { untarCommand . append ( FileUtil . makeShellPath ( inFile ) ) ; } String [ ] shellCmd = { "bash" , "-c" , untarCommand . toString ( ) } ; ShellCommandExecutor shexec = new ShellCommandExecutor ( shellCmd ) ; shexec . execute ( ) ; int exitcode = shexec . getExitCode ( ) ; if ( exitcode != 0 ) { throw new IOException ( "Error untarring file " + inFile + ". Tar process exited with exit code " + exitcode ) ; } }
Given a Tar File as input it will untar the file in a the untar directory passed as the second parameter