Use ZipFile to extract zip files in Java code __java

Source: Internet
Author: User
Tags extract rar file
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 ();
}
	}}
 


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.