Skip to content

Commit f765e65

Browse files
author
Tibo Delor
committed
Zip Benchmark, java.util.zip VS org.apache.commons.compress.archivers.zip
1 parent 8c04e2c commit f765e65

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ repositories{
1111
dependencies{
1212
compile "org.apache.commons:commons-lang3:3.1"
1313
compile 'com.google.caliper:caliper:0.5-rc1'
14-
14+
compile 'org.apache.commons:commons-compress:1.4.1'
1515
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.invalidcodeexception.experiment.zip;
2+
3+
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
4+
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
5+
6+
import java.io.*;
7+
import java.util.zip.ZipEntry;
8+
import java.util.zip.ZipOutputStream;
9+
10+
/**
11+
* User: Tibo
12+
* Date: 7/02/13
13+
*/
14+
public class ZipBenchmark
15+
{
16+
17+
private File dirToCompress = new File("/Users/shameemah/Workspaces/Skaffold/out");
18+
private File fileOutput = new File("/tmp/test.zip");
19+
20+
public static void main(String[] args) throws IOException
21+
{
22+
ZipBenchmark zipBenchmark = new ZipBenchmark();
23+
zipBenchmark.doJavaUtilZip();
24+
zipBenchmark.doCommonsCompressZip();
25+
}
26+
27+
28+
public void doJavaUtilZip() throws IOException
29+
{
30+
long startTime = System.currentTimeMillis();
31+
FileOutputStream out = new FileOutputStream(fileOutput);
32+
javaUtilZipDir(dirToCompress, new ZipOutputStream(out));
33+
out.flush();
34+
out.close();
35+
long endTime = System.currentTimeMillis();
36+
System.out.println("Time :" + (endTime - startTime) + "ms");
37+
System.out.println("File size : "+ fileOutput.getTotalSpace());
38+
}
39+
40+
public void javaUtilZipDir(File dir2zip, ZipOutputStream zos) throws IOException
41+
{
42+
byte[] readBuffer = new byte[2156];
43+
int bytesIn = 0;
44+
for (File f : dir2zip.listFiles())
45+
{
46+
if (f.isDirectory())
47+
{
48+
javaUtilZipDir(f, zos);
49+
}
50+
else
51+
{
52+
FileInputStream fis = new FileInputStream(f);
53+
ZipEntry anEntry = new ZipEntry(f.getPath());
54+
zos.putNextEntry(anEntry);
55+
//now write the content of the file to the ZipOutputStream
56+
while ((bytesIn = fis.read(readBuffer)) != -1)
57+
{
58+
zos.write(readBuffer, 0, bytesIn);
59+
}
60+
//close the Stream
61+
fis.close();
62+
}
63+
64+
}
65+
zos.finish();
66+
}
67+
68+
public void doCommonsCompressZip() throws IOException
69+
{
70+
long startTime = System.currentTimeMillis();
71+
ZipArchiveOutputStream zout = new ZipArchiveOutputStream(fileOutput);
72+
commonsCompressDir(dirToCompress, zout);
73+
zout.flush();
74+
zout.close();
75+
long endTime = System.currentTimeMillis();
76+
System.out.println("Time :" + (endTime - startTime) + "ms");
77+
System.out.println("File size : "+ fileOutput.getTotalSpace());
78+
}
79+
80+
public void commonsCompressDir(File dir2zip, ZipArchiveOutputStream zos) throws IOException
81+
{
82+
byte[] readBuffer = new byte[2156];
83+
int bytesIn = 0;
84+
for (File f : dir2zip.listFiles())
85+
{
86+
if (f.isDirectory())
87+
{
88+
commonsCompressDir(f, zos);
89+
}
90+
else
91+
{
92+
FileInputStream fis = new FileInputStream(f);
93+
ZipArchiveEntry anEntry = new ZipArchiveEntry(f.getPath());
94+
zos.putArchiveEntry(anEntry);
95+
//now write the content of the file to the ZipOutputStream
96+
while ((bytesIn = fis.read(readBuffer)) != -1)
97+
{
98+
zos.write(readBuffer, 0, bytesIn);
99+
}
100+
zos.closeArchiveEntry();
101+
//close the Stream
102+
fis.close();
103+
}
104+
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)