Java automatically decompress the file instance code

Source: Internet
Author: User

Copy codeThe Code is as follows: import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java. util. Enumeration;
Import java.util.zip. ZipEntry;
Import java.util.zip. ZipFile;

Public class UnZipper {

/**
* The function of extracting files to the current directory is equivalent to right-clicking and extracting files
* @ Param zipFile
* @ Param
* @ Author gabriel
*/
@ SuppressWarnings ("rawtypes ")
Public static void unZipFiles (File zipFile) throws IOException {
// Obtain the directory of the compressed file
String path = zipFile. getAbsolutePath ();
Path = path. substring (0, path. lastIndexOf ("\\"));
// System. out. println ("path" + path );
ZipFile zip = new ZipFile (zipFile );
For (Enumeration entries = zip. entries ();
Entries. hasMoreElements ();){
ZipEntry entry = (ZipEntry) entries. nextElement ();
String zipEntryName = entry. getName ();
InputStream in = zip. getInputStream (entry );
// OutPath output directory
String outPath = (path + "\" + zipEntryName). replaceAll ("\\*","/");;
// System. out. println ("outPath" + outPath );
// Determine whether the path exists. If the path does not exist, create a file path.
File file = new File (outPath. substring (0, outPath. lastIndexOf ('/')));
If (! File. exists ()){
File. mkdirs ();
}
// Determine whether the full file path is a folder. If the file has been uploaded, you do not need to decompress the package.
If (new File (outPath). isDirectory ()){
Continue;
}
// Output file path information
System. out. println (outPath );

OutputStream out = new FileOutputStream (outPath );
Byte [] buf1 = new byte [1024];
Int len;
While (len = in. read (buf1)> 0 ){
Out. write (buf1, 0, len );
}
In. close ();
Out. close ();
}
System. out. println ***************** ***");
}

Public static void main (String [] args ){
Try {
UnZipFiles (new File ("D: \ all \ zip \ Default.adiumemoticonset.zip "));
} Catch (IOException e ){
// TODO Auto-generated catch block
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.