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