private static void extractArchive ( ArchiveInputStream input , File destination , FilenameFilter filter ) throws ArchiveExtractionException { ArchiveEntry entry ; try { final String destPath = destination . getCanonicalPath ( ) ; while ( ( entry = input . getNextEntry ( ) ) != null ) { if ( entry . isDirectory ( ) ) { final File dir = new File ( destination , entry . getName ( ) ) ; if ( ! dir . getCanonicalPath ( ) . startsWith ( destPath ) ) { final String msg = String . format ( "Archive contains a path (%s) that would be extracted outside of the target directory." , dir . getAbsolutePath ( ) ) ; throw new AnalysisException ( msg ) ; } if ( ! dir . exists ( ) && ! dir . mkdirs ( ) ) { final String msg = String . format ( "Unable to create directory '%s'." , dir . getAbsolutePath ( ) ) ; throw new AnalysisException ( msg ) ; } } else { extractFile ( input , destination , filter , entry ) ; } } } catch ( IOException | AnalysisException ex ) { throw new ArchiveExtractionException ( ex ) ; } finally { FileUtils . close ( input ) ; } }
Extracts files from an archive .