Some demo here (http://www.example-code.com/java/zip.asp) Make ZIP file
ImportJava. Io .*;
ImportJava.util.zip .*;
ClassMakezipfile {
Public staticVoidMain (string [] ARGs ){
If(ARGs. length! = 2 ){
System. Out. println (
"Usage: Java makezipfile [files to be zipped] [filename After Zip]");
Return;
}
Try{
String filename = ARGs [0];
String zipfilename = ARGs [1];
Makezipfile list =NewMakezipfile ();
List. dozip (filename, zipfilename );
}Catch(Exception e ){
E. printstacktrace ();
}
}
PublicVoidDozip (string filename, string zipfilename ){
Try{
Byte[] Buf =NewByte[1024];
Fileinputstream FCM =NewFileinputstream (filename );
FS. Read (BUF, 0, Buf. Length );
CRC32 CRC =NewCRC32 ();
Zipoutputstream S =NewZipoutputstream (
(Outputstream)NewFileoutputstream (zipfilename ));
S. setlevel (6 );
Zipentry entry =NewZipentry (filename );
Entry. setsize ((Long) BUF. Length );
CRC. Reset ();
CRC. Update (BUF );
Entry. setcrc (CRC. getvalue ());
S. putnextentry (entry );
S. Write (BUF, 0, Buf. Length );
S. Finish ();
S. Close ();
}Catch(Exception e ){
E. printstacktrace ();
}
}
}package
org.kodejava.example.util.zip;
02.
03.
import
java.util.zip.ZipFile;
04.
import
java.util.zip.ZipEntry;
05.
import
java.util.Enumeration;
06.
import
java.io.*;
07.
08.
public
class
ZipFileUnzipDemo {
09.
public
static
void
main(String[] args) {
10.
String zipname =
"data.zip"
;
11.
12.
try
{
13.
ZipFile zipFile =
new
ZipFile(zipname);
14.
Enumeration enumeration = zipFile.entries();
15.
16.
while
(enumeration.hasMoreElements()) {
17.
ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
18.
System.out.println(
"Unzipping: "
+ zipEntry.getName());
19.
20.
BufferedInputStream bis =
new
BufferedInputStream(zipFile.getInputStream(zipEntry));
21.
22.
int
size;
23.
byte
[] buffer =
new
byte
[
2048
];
24.
25.
FileOutputStream fos =
new
FileOutputStream(zipEntry.getName());
26.
BufferedOutputStream bos =
new
BufferedOutputStream(fos, buffer.length);
27.
28.
while
((size = bis.read(buffer,
0
, buffer.length)) != -
1
) {
29.
bos.write(buffer,
0
, size);
30.
}
31.
32.
bos.flush();
33.
bos.close();
34.
fos.close();
35.
36.
bis.close();
37.
}
38.
}
catch
(IOException e) {
39.
e.printStackTrace();
40.
}
41.
}
42.
}