Skip to content

Instantly share code, notes, and snippets.

@cstamas
Created February 9, 2011 13:19
Show Gist options
  • Save cstamas/818453 to your computer and use it in GitHub Desktop.
Save cstamas/818453 to your computer and use it in GitHub Desktop.
ZipFile vs ZipInputStream
=========================
Tested against ZIP file having 829 entries and 28783146 bytes big (compressed size).
Source code attached to the end of this gist.
Platform: Mac OSX 10.6.6,
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04-307-10M3261)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03-307, mixed mode)
ZipInputStream
==============
cstamas@marvin target$ java -jar ziptest-1.0.0-SNAPSHOT.jar stream nexus-oss-webapp-1.10-SNAPSHOT-bundle.zip
ZipInputStream
==============
nexus-oss-webapp-1.10-SNAPSHOT/
nexus-oss-webapp-1.10-SNAPSHOT/runtime/
nexus-oss-webapp-1.10-SNAPSHOT/runtime/apps/
.....
Run1:
=====
Total of 829 entries.
Done in 759 millis.
Final Memory: 2063kB/83008kB
Final Memory: 763kB/83008kB (after GC)
Run2:
=====
Total of 829 entries.
Done in 747 millis.
Final Memory: 2063kB/83008kB
Final Memory: 763kB/83008kB (after GC)
Run3:
=====
Total of 829 entries.
Done in 783 millis.
Final Memory: 2063kB/83008kB
Final Memory: 763kB/83008kB (after GC)
ZipFile
=======
cstamas@marvin target$ java -jar ziptest-1.0.0-SNAPSHOT.jar zipfile nexus-oss-webapp-1.10-SNAPSHOT-bundle.zip
ZipFile
=======
nexus-oss-webapp-1.10-SNAPSHOT/
nexus-oss-webapp-1.10-SNAPSHOT/runtime/
nexus-oss-webapp-1.10-SNAPSHOT/runtime/apps/
.....
Run1:
=====
Total of 829 entries.
Done in 80 millis.
Final Memory: 2067kB/83008kB
Final Memory: 771kB/83008kB (after GC)
Run2:
=====
Total of 829 entries.
Done in 70 millis.
Final Memory: 2067kB/83008kB
Final Memory: 1112kB/83008kB (after GC)
Run3:
=====
Total of 829 entries.
Done in 74 millis.
Final Memory: 2067kB/83008kB
Final Memory: 771kB/83008kB (after GC)
Conclusion
==========
Memory consumption: exactly same.
Speed: ZipFile is for orders of magnitude faster that ZipInputStream.
Code
====
package org.sonatype.ziptest;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class App
{
public static final long KB = 1024;
public static final long MB = 1024 * 1024;
public static void main( String[] args )
throws IOException
{
final long started = System.currentTimeMillis();
if ( args != null && args.length == 2 )
{
final File file = new File( args[1] );
final String sw = args[0];
if ( "zipfile".equalsIgnoreCase( sw ) )
{
testZipFile( file );
}
else
{
testZipInputStream( file );
}
}
else
{
System.out.println( "java -jar thisjar.jar [1|2] <aZipFile>" );
}
System.out.println( String.format( "Done in %s millis.", System.currentTimeMillis() - started ) );
Runtime r = Runtime.getRuntime();
System.out.println( "Final Memory: " + ( r.totalMemory() - r.freeMemory() ) / KB + "kB/" + r.totalMemory() / KB
+ "kB" );
System.gc();
System.out.println( "Final Memory: " + ( r.totalMemory() - r.freeMemory() ) / KB + "kB/" + r.totalMemory() / KB
+ "kB (after GC)" );
}
public static void testZipFile( final File file )
throws IOException
{
System.out.println( "ZipFile" );
System.out.println( "=======" );
ZipFile zf = new ZipFile( file );
Enumeration<? extends ZipEntry> entries = zf.entries();
int count = 0;
while ( entries.hasMoreElements() )
{
ZipEntry entry = entries.nextElement();
System.out.println( entry.getName() );
count++;
}
System.out.println( "=====" );
System.out.println( String.format( "Total of %s entries.", count ) );
}
public static void testZipInputStream( final File file )
throws IOException
{
System.out.println( "ZipInputStream" );
System.out.println( "==============" );
ZipInputStream zi = new ZipInputStream( new BufferedInputStream( new FileInputStream( file ) ) );
ZipEntry entry = null;
int count = 0;
while ( ( entry = zi.getNextEntry() ) != null )
{
System.out.println( entry.getName() );
count++;
}
System.out.println( "=====" );
System.out.println( String.format( "Total of %s entries.", count ) );
}
}
@nicoulaj
Copy link

nicoulaj commented Feb 9, 2011

Have you tested it against smaller/bigger zip files ?

@cstamas
Copy link
Author

cstamas commented Feb 9, 2011

No, this "benchmark" was not a real one. See the updated project (added TrueZip too) project for more

https://github.com/cstamas/zip-benchmarks

Feel free to play with it!

@nicoulaj
Copy link

nicoulaj commented Feb 9, 2011

Yes, I saw it after I commented. Nice initiative !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment