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