Decompress ZIP using Java

Source: Internet
Author: User

Import java. Io. bufferedinputstream;
Import java. Io. bufferedoutputstream;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. util. enumeration;
Import java.util.zip. zipentry;
Import java.util.zip. zipfile;

Public class zipper {
Static final int buffer = 2048;

Public static void main (string [] argv ){
Try {
String filename = "d :\\ lib.zip ";
String filepath = "d :\\";
Zipfile = new zipfile (filename );
Enumeration EMU = zipfile. Entries ();
Int I = 0;
While (EMU. hasmoreelements ()){
Zipentry entry = (zipentry) EMU. nextelement ();
// Read the directory as a file once, so you only need to create a directory, and the files under it will be iterated.
If (entry. isdirectory ()){
New file (filepath + entry. getname (). mkdirs ();
Continue;
}
Bufferedinputstream Bis = new bufferedinputstream (zipfile. getinputstream (entry ));
File file = new file (filepath + entry. getname ());
// The reason for this is that zipfile reads files randomly, which may first read a file.
// The directory where the file is located has not appeared, so you need to create a directory.
File parent = file. getparentfile ();
If (parent! = NULL &&(! Parent. exists ())){
Parent. mkdirs ();
}
Fileoutputstream Fos = new fileoutputstream (File );
Bufferedoutputstream Bos = new bufferedoutputstream (FOS, buffer );
Int count;
Byte [] DATA = new byte [buffer];
While (COUNT = bis. Read (data, 0, buffer ))! =-1 ){
Bos. Write (data, 0, count );
}
Bos. Flush ();
Bos. Close ();
Bis. Close ();
}
Zipfile. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}

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.