Java decompress the file

Source: Internet
Author: User

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 ....................");
}

}

Related Article

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.