Java Extract Files

Source: Internet
Author: User

Need Import Org.apache.tools.zip.ZipEntry;

import Org.apache.tools.zip.ZipFile;

Unzip the file

Ant.jar's http://download.csdn.net/detail/qq490691606/7935909

Configuration in Maven

 <            Dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.4.2</version> <scope>system</scope> <systempath>${j ava.home}/. /lib/tools.jar</systempath> </dependency> 
public class Unziputil {private static void CreateFile (String path) {file Dir = new File (path);        if (!dir.exists ()) {Dir.mkdir (); }}//Get 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 file object to unzip        File = new file (Zipfilepath);        Create a zip file object ZipFile zipfile = new ZipFile (file);        Create this zip file to extract the directory file Unzipfile = new file (Unzipfilepath + "/" + GetFileName (File.getname ()));        Extract file does not exist then create if (!unzipfile.exists ()) unzipfile.mkdirs ();        Get the ZIP File entry Enumeration Object Enumeration Zipenum = Zipfile.getentries ();        Defines the input and output stream object InputStream input = null;        OutputStream output = null;        Defines the object zipentry entry = null; Loop read entry while (Zipenum.hasmoreelements ()) {//get current Entry Entry = (ZipEntry) zipenum.nextelement ();            String entryName = new String (Entry.getname ());            Use/Separate entry name String names[] = Entryname.split ("\\/");            int length = Names.length;            String path = Unzipfile.getabsolutepath (); for (int v = 0; v < length; v++) {if (v < length-1) {//directory before last directory path + = "/                    "+ Names[v] +"/";                CreateFile (path); } else {///The Last if (Entryname.endswith ("/"))//directory, create folder CreateFile (unzipfile                    . GetAbsolutePath () + "/" + entryName);                        else {//for file, output to 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);                        Close flow input.close ();                        Writes the cache to the hard disk Output.flush ();                    Output.close (); }                }            }        }    }}



Java Extract Files

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.