Java code decompress the zip file and java decompress the zip file

Source: Internet
Author: User

Java code decompress the zip file and java decompress the zip file

Import java. io. File;

Import java. io. FileOutputStream;

Import java. io. IOException;

Import java. io. InputStream;

Import java. util. Enumeration;

Import org.apache.tools.zip. ZipEntry;

Import org.apache.tools.zip. ZipFile;

Import org. springframework. stereotype. Service;

Import org. springframework. transaction. annotation. Transactional;

/**
* @ Date Creation Time: 11:06:46, January 1, September 25, 2016
* @ Version 1.0
* @ Parameter
* @ Since 11:06:46, January 1, September 25, 2016
* @ Return
*/
Public class unZipFiles {

// Zip file path
String fileAddress = "D: \ test.zip ";
// Zip file decompression address
String unZipAddress = "F :\\ unZipFiles \\";

// Search for files in the directory
File file = new File (fileAddress );
ZipFile zipFile = null;
Try {
ZipFile = new ZipFile (file, "GBK"); // sets the encoding format.
} Catch (IOException exception ){
Exception. printStackTrace ();
System. out. println ("the extracted file does not exist! ");
}

Enumeration e = zipFile. getEntries ();
While (e. hasMoreElements ()){
ZipEntry zipEntry = (ZipEntry) e. nextElement ();
If (zipEntry. isDirectory ()){
String name = zipEntry. getName ();
Name = name. substring (0, name. length ()-1 );
File f = new File (unZipAddress + name );
F. mkdirs ();
} Else {
File f = new File (unZipAddress + zipEntry. getName ());
F. getParentFile (). mkdirs ();
F. createNewFile ();
InputStream is = zipFile. getInputStream (zipEntry );
FileOutputStream fos = new FileOutputStream (f );
Int length = 0;
Byte [] B = new byte [1024];
While (length = is. read (B, 0, 1024 ))! =-1 ){
Fos. write (B, 0, length );
}
Is. close ();
Fos. close ();
}
}
If (zipFile! = Null ){
ZipFile. close ();
}
File. deleteOnExit (); // Delete the compressed package after decompression
}

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.