Public void testreadzip () throws exception {
// Inputstream is = new bufferedinputstream (New fileinputstream ());
String basedir = "C: // Documents and Settings // ws // desktop // create a folder ";
Zipfile zfile = new zipfile ("C: // Documents and Settings // ws // desktop // new folder // dd.zip ");
System. Out. println (zfile. getname ());
Enumeration zlist = zfile. Entries ();
Zipentry ze = NULL;
Byte [] Buf = new byte [1, 1024];
While (zlist. hasmoreelements ()){
// Obtain a zipentry from zipfile
Ze = (zipentry) zlist. nextelement ();
If (ze. isdirectory ()){
System. Out. println ("dir:" + Ze. getname () + "skipped ..");
Continue;
}
System. Out. println ("extracting:" + Ze. getname () + "/t"
+ Ze. getsize () + "/t" + Ze. getcompressedsize ());
// Obtain an inputstream with the zipentry parameter and write it to outputstream.
Outputstream OS = new bufferedoutputstream (New fileoutputstream (
Getrealfilename (basedir, ze. getname ())));
Inputstream is = new bufferedinputstream (zfile. getinputstream (ze ));
Int readlen = 0;
While (readlen = is. Read (BUF, 0, 1024 ))! =-1 ){
OS. Write (BUF, 0, readlen );
}
Is. Close ();
OS. Close ();
System. Out. println ("extracted:" + Ze. getname ());
}
Zfile. Close ();
}
Private file getrealfilename (string basedir, string absfilename) {// specify the root directory // relative path name -- name in zipentry
String [] dirs = absfilename. Split ("/");
// System. Out. println (dirs. Length );
File ret = new file (basedir );
If (dirs. length> 1 ){
For (INT I = 0; I <dirs. Length-1; I ++ ){
Ret = new file (Ret, dirs [I]);
}
}
If (! Ret. exists ()){
Ret. mkdirs ();
}
Ret = new file (Ret, dirs [dirs. Length-1]);
Return ret;
}