private static void jar ( File src , String prefix , JarInfo info ) throws IOException { JarOutputStream jout = info . out ; if ( src . isDirectory ( ) ) { // create / init the zip entry prefix = prefix + src . getName ( ) + "/" ; ZipEntry entry = new ZipEntry ( prefix ) ; entry . setTime ( src . lastModified ( ) ) ; entry . setMethod ( JarOutputStream . STORED ) ; entry . setSize ( 0L ) ; entry . setCrc ( 0L ) ; jout . putNextEntry ( entry ) ; jout . closeEntry ( ) ; // process the sub-directories File [ ] files = src . listFiles ( info . filter ) ; for ( int i = 0 ; i < files . length ; i ++ ) { jar ( files [ i ] , prefix , info ) ; } } else if ( src . isFile ( ) ) { // get the required info objects byte [ ] buffer = info . buffer ; // create / init the zip entry ZipEntry entry = new ZipEntry ( prefix + src . getName ( ) ) ; entry . setTime ( src . lastModified ( ) ) ; jout . putNextEntry ( entry ) ; // dump the file FileInputStream in = new FileInputStream ( src ) ; int len ; while ( ( len = in . read ( buffer , 0 , buffer . length ) ) != - 1 ) { jout . write ( buffer , 0 , len ) ; } in . close ( ) ; jout . closeEntry ( ) ; } }
This recursive method writes all matching files and directories to the jar output stream .