Java decompress the file

Source: Internet
Author: User

Java decompress the file

 

Import org.apache.tools.zip. ZipEntry; 

 

Import org.apache.tools.zip. ZipFile;

Decompress the file

 

Configuration in maven

 

        
             
  
   com.sun
              tools            
  
   1.4.2
              
  
   system
              
  
   ${java.home}/../lib/tools.jar
          
 
Public class UnzipUtil {private static void createFile (String path) {File dir = new File (path); if (! Dir. exists () {dir. mkdir () ;}// get the file name private static String getFileName (String name) {return name. substring (0, name. lastIndexOf (.));} public static void unzip (String zipFilePath, String unzipFilePath) throws Exception {// create a File file File to be decompressed = new File (zipFilePath ); // create the zip file object ZipFile zipFile = new ZipFile (File); // create the zip File decompression directory file unzipFile = new file (unzipFilePath ++ getFileName (File. get Name (); // if (! UnzipFile. exists () unzipFile. mkdirs (); // obtain the Enumeration zipEnum = zipFile. getEntries (); // defines the input and output stream object InputStream input = null; OutputStream output = null; // defines the object ZipEntry entry = null; // reads the entry while (zipEnum. hasMoreElements () {// obtain the current entry = (ZipEntry) zipEnum. nextElement (); String entryName = new String (entry. getName (); // use/to separate the entry names String names [] = entryName. split (\/); int le Ngth = names. length; String path = unzipFile. getAbsolutePath (); for (int v = 0; v <length; v ++) {if (v <length-1) {// path + =/+ names [v] +/; createFile (path);} else {// The last if (entryName. endsWith (/) // is the directory, then create the folder createFile (unzipFile. getAbsolutePath () +/+ entryName); else {// indicates the file, which is output to the file input = zipFile. getInputStream (entry); output = new FileOutputStream (new File (unzipF Ile. 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); // close the stream input. close (); // write the cache to the hard disk output. flush (); output. close ();}}}}}}



 

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.