private boolean unZip ( String innerDir , String archiveFile ) { boolean success = true ; ZipFile zipFile ; try { zipFile = new ZipFile ( archiveFile ) ; // Get the list of file headers from the zip file before unpacking List fileHeaderList = zipFile . getFileHeaders ( ) ; List < PathMatcher > matchers = Arrays . stream ( filesExcludes ) . map ( fileExclude -> FileSystems . getDefault ( ) . getPathMatcher ( GLOB_PREFIX + fileExclude ) ) . collect ( Collectors . toList ( ) ) ; // Loop through the file headers and extract only files that are not matched by fileExcludes patterns for ( int i = 0 ; i < fileHeaderList . size ( ) ; i ++ ) { FileHeader fileHeader = ( FileHeader ) fileHeaderList . get ( i ) ; String fileName = fileHeader . getFileName ( ) ; if ( filesExcludes . length > 0 ) { Predicate < PathMatcher > matchesExcludes = pathMatcher -> pathMatcher . matches ( Paths . get ( innerDir , fileName ) ) ; if ( matchers . stream ( ) . noneMatch ( matchesExcludes ) ) { zipFile . extractFile ( fileHeader , innerDir ) ; } } else { zipFile . extractFile ( fileHeader , innerDir ) ; } } } catch ( Exception e ) { success = false ; logger . warn ( "Error extracting file {}: {}" , archiveFile , e . getMessage ( ) ) ; logger . debug ( "Error extracting file {}: {}" , archiveFile , e . getStackTrace ( ) ) ; } finally { // remove reference to zip file zipFile = null ; } return success ; }
Open and extract data from zip pattern files