public static void zipDirectory ( Logger log , File directory , ZipOutputStream zos , String path , FileFilter filter ) throws IOException { // get a listing of the directory content File [ ] dirList = directory . listFiles ( ) ; byte [ ] readBuffer = new byte [ 8192 ] ; int bytesIn = 0 ; // loop through dirList, and zip the files if ( dirList != null ) { for ( File f : dirList ) { if ( f . isDirectory ( ) ) { String prefix = path + f . getName ( ) + "/" ; if ( matches ( filter , f ) ) { zos . putNextEntry ( new ZipEntry ( prefix ) ) ; zipDirectory ( log , f , zos , prefix , filter ) ; } } else { String entry = path + f . getName ( ) ; if ( matches ( filter , f ) ) { FileInputStream fis = new FileInputStream ( f ) ; try { ZipEntry anEntry = new ZipEntry ( entry ) ; zos . putNextEntry ( anEntry ) ; bytesIn = fis . read ( readBuffer ) ; while ( bytesIn != - 1 ) { zos . write ( readBuffer , 0 , bytesIn ) ; bytesIn = fis . read ( readBuffer ) ; } } finally { fis . close ( ) ; } if ( log . isDebugEnabled ( ) ) { log . debug ( "zipping file " + entry ) ; } } } zos . closeEntry ( ) ; } } }
Zips the directory recursively into the ZIP stream given the starting path and optional filter