Poor Performance of Java's unzip utilities -


i have noticed unzip facility in java extremely slow compared using native tool such winzip.

is there third party library available java more efficient? open source preferred.

edit

here speed comparison using java built-in solution vs 7zip. added buffered input/output streams in original solution (thanks jim, did make big difference).

zip file size: 800k java solution: 2.7 seconds 7zip solution: 204 ms

here modified code using built-in java decompression:

/** unpacks give zip file using built in java facilities unzip. */ @suppresswarnings("unchecked") public final static void unpack(file zipfile, file rootdir) throws ioexception {   zipfile zip = new zipfile(zipfile);   enumeration<zipentry> entries = (enumeration<zipentry>) zip.entries();   while(entries.hasmoreelements()) {     zipentry entry = entries.nextelement();     java.io.file f = new java.io.file(rootdir, entry.getname());     if (entry.isdirectory()) { // if directory, create       continue;     }      if (!f.exists()) {       f.getparentfile().mkdirs();       f.createnewfile();     }      bufferedinputstream bis = new bufferedinputstream(zip.getinputstream(entry)); // input stream     bufferedoutputstream bos = new bufferedoutputstream(new java.io.fileoutputstream(f));     while (bis.available() > 0) {  // write contents of 'is' 'fos'       bos.write(bis.read());     }     bos.close();     bis.close();   } } 

the problem not unzipping, it's inefficient way write unzipped data disk. benchmarks show using

    inputstream = zip.getinputstream(entry); // input stream     outputstream os = new java.io.fileoutputstream(f);     byte[] buf = new byte[4096];     int r;     while ((r = is.read(buf)) != -1) {       os.write(buf, 0, r);     }     os.close();     is.close(); 

instead reduces method's execution time factor of 5 (from 5 1 second 6 mb zip file).

the culprit use of bis.available(). aside being incorrect (available returns number of bytes until call read block, not until end of stream), bypasses buffering provided bufferedinputstream, requiring native system call every byte copied output file.

note wrapping in bufferedstream not necessary if use bulk read , write methods above, , code close resources not exception safe (if reading or writing fails reason, neither is nor os closed). finally, if have ioutils in class path, recommend using tested ioutils.copy instead of rolling own.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -