public static void extractFiles ( File archive , File extractTo , Engine engine ) throws ExtractionException { if ( archive == null || extractTo == null ) { return ; } final String destPath ; try { destPath = extractTo . getCanonicalPath ( ) ; } catch ( IOException ex ) { throw new ExtractionException ( "Unable to extract files to destination path" , ex ) ; } ZipEntry entry ; try ( FileInputStream fis = new FileInputStream ( archive ) ; BufferedInputStream bis = new BufferedInputStream ( fis ) ; ZipInputStream zis = new ZipInputStream ( bis ) ) { while ( ( entry = zis . getNextEntry ( ) ) != null ) { if ( entry . isDirectory ( ) ) { final File d = new File ( extractTo , entry . getName ( ) ) ; if ( ! d . getCanonicalPath ( ) . startsWith ( destPath ) ) { final String msg = String . format ( "Archive (%s) contains a path that would be extracted outside of the target directory." , archive . getAbsolutePath ( ) ) ; throw new ExtractionException ( msg ) ; } if ( ! d . exists ( ) && ! d . mkdirs ( ) ) { final String msg = String . format ( "Unable to create '%s'." , d . getAbsolutePath ( ) ) ; throw new ExtractionException ( msg ) ; } } else { final File file = new File ( extractTo , entry . getName ( ) ) ; if ( engine == null || engine . accept ( file ) ) { if ( ! file . getCanonicalPath ( ) . startsWith ( destPath ) ) { final String msg = String . format ( "Archive (%s) contains a file that would be extracted outside of the target directory." , archive . getAbsolutePath ( ) ) ; throw new ExtractionException ( msg ) ; } try ( FileOutputStream fos = new FileOutputStream ( file ) ) { IOUtils . copy ( zis , fos ) ; } catch ( FileNotFoundException ex ) { LOGGER . debug ( "" , ex ) ; final String msg = String . format ( "Unable to find file '%s'." , file . getName ( ) ) ; throw new ExtractionException ( msg , ex ) ; } catch ( IOException ex ) { LOGGER . debug ( "" , ex ) ; final String msg = String . format ( "IO Exception while parsing file '%s'." , file . getName ( ) ) ; throw new ExtractionException ( msg , ex ) ; } } } } } catch ( IOException ex ) { final String msg = String . format ( "Exception reading archive '%s'." , archive . getName ( ) ) ; LOGGER . debug ( "" , ex ) ; throw new ExtractionException ( msg , ex ) ; } }
Extracts the contents of an archive into the specified directory . The files are only extracted if they are supported by the analyzers loaded into the specified engine . If the engine is specified as null then all files are extracted .