Compression and decompression of ZIP files using ant implementation zip file

Source: Internet
Author: User

Java uses ant to implement ZIP files to compress and extract zip files.

A single file is compressed into a zip file, and multiple files are compressed into a zip file.

You can compress large files.

You need to use the Ant.jar file and put it in the Lib directory.

Ant.jar Download: http://download.csdn.net/detail/xinxin19881112/8906157


Zip.java

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.OutputStream;
Import Java.util.zip.ZipOutputStream;

Import Org.apache.tools.zip.ZipEntry; /** * Zip Operation Tool class/public class Zip {/** * compressed file list to a zip file * @param zipfilename zip file to compress @param paths file list, multi-parameter Number * @throws Exception */public static void compress (String zipfilename, String ... paths) throws Exception {com
	Press (New FileOutputStream (zipfilename), paths); /** * Compressed file list to output stream * @param os to compress to stream * @param paths file list, Multi-parameter * @throws Exception/public static void Compr
		ESS (OutputStream OS, String ... paths) throws Exception {Zipoutputstream Zos = new Zipoutputstream (OS);
			for (String path:paths) {if (Path.equals ("")) continue;
			Java.io.File File = new Java.io.File (path); if (file.exists ()) {if (File.isdirectory ()) {zipdirectory (Zos, File.getpath (), file.getname () + file.sep
				Arator); else {zipfile (Zos, filE.getpath (), "");
	}} zos.close (); private static void Zipdirectory (Zipoutputstream zos, String dirname, String basepath) throws Exception {File D
		IR = new File (dirname);
			if (dir.exists ()) {File files[] = Dir.listfiles (); if (Files.length > 0) {for (File file:files) {if (File.isdirectory ()) {zipdirectory (Zos, FILE.GETPA
								Th (), BasePath + file.getname (). substring (File.getname (). LastIndexOf (File.separator) + 1)
					+ File.separator);
				else ZipFile (Zos, File.getpath (), basepath);
				} else {zipentry ze = new ZipEntry (basepath);
			Zos.putnextentry (Ze);  }} private static void ZipFile (Zipoutputstream zos, string filename, String basepath) throws Exception {File
		File = new file (filename);
			if (file.exists ()) {FileInputStream FIS = new FileInputStream (filename);
			ZipEntry ze = new ZipEntry (BasePath + file.getname ());
			Zos.putnextentry (Ze); Byte[] Buffer = new byte[8192];
			int count = 0;
			while ((count = fis.read (buffer)) > 0) {zos.write (buffer, 0, count);
		} fis.close (); }
	}
}



Testcompress.java

Import Java.io.FileOutputStream;

Import Com.xx.commmon.Zip;

/**
 * Compressed file test
/public class Testcompress {public

	static void Main (string[] args) {
		//list of files to compress
		String path01 = "e:\\ download \\cn_windows_8_x86_dvd_915414.iso";
		String path02 = "e:\\ppt template _v0.1.potx";
		try {
			FileOutputStream os = new FileOutputStream ("e:\\ test. zip");//output ZIP file stream
			zip.compress (OS, path01);
		} catch (Exception e) {
			e.printstacktrace ();}}}




Testectractzip.java

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.util.ArrayList;
Import Java.util.zip.ZipEntry;

Import Java.util.zip.ZipInputStream; /** * Decompression test/public class Testectractzip {@SuppressWarnings ({"Unchecked", "static-access"}) public static void
		Main (string[] args) {Testectractzip z = new Testectractzip (); Arraylist<string> a = z.ectract ("C:\\a.zip", "c:\\tmp\\");
		Returns the extracted file list for (String s:a) {System.out.println (s); /** * Decompression * @param szippathfile to extract the file * @param Sdestpath extract to a folder * @return/@SuppressWarnings ("Unchec ked ") public static ArrayList ectract (String szippathfile, String sdestpath) {arraylist<string> allfilename = NE
		W arraylist<string> ();
			try {///First specify the location and filename of the compressed file, establish the FileInputStream object FileInputStream fins = new FileInputStream (szippathfile);
			The fins is passed into the zipinputstream zipinputstream Zins = new Zipinputstream (fins);
			ZipEntry ze = null; byte[] ch = New BYTE[256];
				while (ze = zins.getnextentry ())!= null) {file Zfile = new File (Sdestpath + ze.getname ());
				File Zfile = new file (Sdestpath +file.separator+ ze.getname ());
				File Fpath = new file (Zfile.getparentfile (). GetPath ());
					if (Ze.isdirectory ()) {if (!zfile.exists ()) zfile.mkdirs ();
				Zins.closeentry ();
					else {if (!fpath.exists ()) fpath.mkdirs ();
					FileOutputStream Fouts = new FileOutputStream (zfile);
					int i;
					Allfilename.add (Zfile.getabsolutepath ());
					while ((i = Zins.read (ch))!=-1) fouts.write (CH, 0, I);
					Zins.closeentry ();
				Fouts.close ();
			} fins.close ();
		Zins.close ();
		catch (Exception e) {System.err.println ("Extract error:" + e.getmessage ());
	return allfilename; }
}


NEW: http://blog.csdn.net/xinxin19881112/article/details/46913931

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.