Java extract zip file key code _java

Source: Internet
Author: User
Tags extract zip file

No more nonsense, to put the key code for everyone, the specific code as follows:

 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 Date: September 25, 2016 Morning 11:06:46 * @version 1.0 * @parameter * @since September 25, 2016 morning 11:06:46 * @return * * Public
  Class Unzipfiles {//zip file path String fileaddress = "D:\\test.zip";
  ZIP file decompression address String unzipaddress = "f:\\unzipfiles\\";
  Go to the directory to look for files file = new filename (fileaddress);
  ZipFile zipfile = null;
    try {zipfile = new ZipFile (file, GBK);//Set encoding format} catch (IOException exception) {exception.printstacktrace ();
  System.out.println ("Extract 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 () ()//after decompression, delete the compressed package} 

OK, the code is over, the above is a small set to introduce Java extract zip file key code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.