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
}