Java decompress the file
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java. util. Enumeration;
Import org.apache.tools.zip. ZipEntry;
Import org.apache.tools.zip. ZipFile;
Public class Zip2File {
/**
* Create a directory
* @ Param path
* Absolute directory path name
*/
Static void createDir (String path ){
File dir = new File (path );
If (dir. exists () = false)
Dir. mkdir ();
}
/**
* Decompress the zip file.
* @ Param zipFilePath
* Absolute zip file path
* @ Param unzipDirectory
* Decompress the package to confirm
* @ Throws Exception
*/
@ SuppressWarnings ("rawtypes ")
Public static void unzip (String zipFilePath, String unzipDirectory) throws Exception {
File file = new File (zipFilePath );
ZipFile zipFile = new ZipFile (file );
// Create a directory for extracting the zip file
File unzipFile = new File (unzipDirectory );
If (unzipFile. exists ())
UnzipFile. delete ();
UnzipFile. mkdir ();
// Obtain the zip file entry enumeration object
Enumeration zipEnum = zipFile. getEntries ();
// Define the input/output stream object
InputStream input = null;
OutputStream output = null;
// Define the object
ZipEntry entry = null;
// Read entries cyclically
While (zipEnum. hasMoreElements ()){
// Obtain the current entry
Entry = (ZipEntry) zipEnum. nextElement ();
String entryName = new String (entry. getName ());
// Use/to separate the entry names
String names [] = entryName. split ("\\/");
Int length = names. length;
String path = unzipFile. getAbsolutePath ();
System. out. println (path + "----");
For (int v = 0; v <length; v ++ ){
If (v <length-1) {// The directory before the last directory
Path + = "/" + names [v] + "/";
CreateDir (path );
} Else {// last
If (entryName. endsWith ("/") // directory, create a folder
CreateDir (unzipFile. getAbsolutePath () + "/" + entryName );
Else {// is a file, which is output to the file
Input = zipFile. getInputStream (entry );
Output = new FileOutputStream (new File (unzipFile. getAbsolutePath () + "/" + entryName ));
Byte [] buffer = new byte [1024*8];
Int readLen = 0;
While (readLen = input. read (buffer, 0, 1024*8 ))! =-1)
Output. write (buffer, 0, readLen );
Input. close ();
Output. flush ();
Output. close ();
}
}
}
}
}
Public static void main (String [] args) throws Exception {
Unzip ("D :\\ 1.zip"," D :\\ new ");
System. out. println ("over ....................");
}
}