Use the Java.util.zip.ZipFile class to extract the files.
Can extract Zip,jar,war, but cannot extract RAR file.
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.Enumeration;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipFile;
public class UnZip {static final int BUFFER = 1024;
public void unzip (file Unzipdir, file Zip) throws IOException {if (!zip.exists ()) {return;
} if (!unzipdir.exists ()) {unzipdir.mkdirs ();
} if (!unzipdir.isdirectory ()) {return;
int filecount = 0;
ZipFile zipfile = new ZipFile (Zip);
Bufferedoutputstream ds = null;
Bufferedinputstream is = null;
ZipEntry entry = null;
Enumeration entries = Zipfile.entries ();
System.out.println ("Start ...");
while (Entries.hasmoreelements ()) {try {entry = (ZipEntry) entries.nextelement ();
System.out.println ((++filecount) + ") Extracting:" + entry);
File Tempfile = new file (Unzipdir.getabsolutepath () + "/" + entry.getname ()); if (ENTRY.ISDIRectory ()) {tempfile.mkdirs ();
Continue
else if (!tempfile.getparentfile (). exists ()) {Tempfile.getparentfile (). Mkdirs ();
is = new Bufferedinputstream (Zipfile.getinputstream (entry));
int count;
byte data[] = new Byte[buffer];
FileOutputStream fos = new FileOutputStream (unzipdir. GetAbsolutePath () + "/" + entry.getname ());
ds = new Bufferedoutputstream (FOS, BUFFER);
while (count = is.read (data, 0, BUFFER))!=-1) {ds.write (data, 0, count);
} ds.flush ();
catch (IOException ex) {throw ex;
Finally {try {if (ds!= null) ds.close ();
if (is!= null) is.close ();
catch (IOException e) {//Ignore}}} System.out.println ("Finished!");
public static void Main (string[] args) {UnZip UnZip = new UnZip ();
try {unzip.unzip (new file ("c:/unzip/"), New file ("C:/stockquote1.war");
catch (Exception e) {e.printstacktrace ();
}
}}