Java decompress the file

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.