-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZipCompress.java
More file actions
92 lines (69 loc) · 2.57 KB
/
ZipCompress.java
File metadata and controls
92 lines (69 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package io;
import java.io.*;
import java.util.zip.*;
import java.util.*;
/**
* RUN:
* javac io/ZipCompress.java && java io.ZipCompress io/ZipCompress.java
*
* OUTPUT:
*
*/
public class ZipCompress {
public static void main(String[] args) throws IOException {
FileOutputStream f = new FileOutputStream("test.zip");
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum);
BufferedOutputStream out = new BufferedOutputStream(zos);
zos.setComment("Check ZIP-compressing Java"); // but Java have no method getComment()
for (String arg : args) {
System.out.println("Writing file " + arg);
BufferedReader in = new BufferedReader(new FileReader(arg));
zos.putNextEntry(new ZipEntry(arg));
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.flush();
}
out.close();
// Checksum is availabel ONLY after zip file closing !
System.out.println("Checksum " + csum.getChecksum().getValue());
// Unziping archive
System.out.println("Reading zip-file");
FileInputStream fi = new FileInputStream("test.zip");
CheckedInputStream csum1 = new CheckedInputStream(fi, new Adler32());
ZipInputStream in2 = new ZipInputStream(csum1);
BufferedInputStream bis = new BufferedInputStream(in2);
ZipEntry ze;
while ((ze = in2.getNextEntry()) != null) {
System.out.println("Reading file " + ze);
int x;
while ((x = bis.read()) != -1) {
System.out.write(x);
}
}
if (args.length == 1) {
System.out.println("\nChecksum: " + csum1.getChecksum().getValue());
}
bis.close();
System.out.println();
System.out.println("Alternate method reading zip=files");
System.out.println();
// Alternate method reading zip=files
ZipFile zf = new ZipFile("test.zip");
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry)e.nextElement();
System.out.println("File: " + ze2);
InputStream input = zf.getInputStream(ze2);
int x;
while ((x = input.read()) != -1) {
System.out.write(x);
}
input.close();
}
zf.close();
}
}