Easy file compression/decompression with Apache Ant

Source: Internet
Author: User

In the process of learning the Apache ant document, see the Using Apache ant™tasks Outside of Ant ,

It describes how to use the Expand class in the Ant.jar package to implement the decompression of the zip file.

found that the decompression file is so easy, according to gourd painting ladle to achieve compressed file is also easy.

Import Java.io.File;
Import org.apache.tools.ant.BuildException;
Import Org.apache.tools.ant.Project;
Import Org.apache.tools.ant.taskdefs.Expand;
Import Org.apache.tools.ant.taskdefs.Zip;

Import Org.apache.tools.ant.types.FileSet;

	public class Zipper {Public final static String encoding = "GBK"; Compress public static void Zip (String srcpathname, String zipfilepath) throws Buildexception, RuntimeException {File
		File = new file (srcpathname);

		if (!file.exists ()) throw new RuntimeException ("source file or directory" + Srcpathname + "does not exist.");
		Project proj = new Project ();
		Fileset fileset = new Fileset ();
		Fileset.setproject (proj);
			Determines whether the directory or file if (File.isdirectory ()) {fileset.setdir (file);
			Ant Include/exclude rules can be used here//For example://Fileset.setexcludes ("**/*.txt");
		Fileset.setincludes ("**/*.xls");
		else {fileset.setfile (file);
		} Zip Zip = new zip ();
		Zip.setproject (proj);
		Zip.setdestfile (New File (Zipfilepath)); ZiP.addfileset (Fileset);
		zip.setencoding (encoding);
	Zip.execute ();  }//Uncompress public static void unzip (string zipfilepath, String destdir) throws Buildexception, RuntimeException {if

		(!new file (Zipfilepath). Exists ()) throw new RuntimeException ("Zip File" + Zipfilepath + "does not exist.");
		Project proj = new Project ();
		Expand Expand = new Expand ();
		Expand.setproject (proj);
		Expand.settasktype ("unzip");
		Expand.settaskname ("unzip");

		expand.setencoding (encoding);
		EXPAND.SETSRC (New File (Zipfilepath));
		Expand.setdest (New File (Destdir));
	Expand.execute (); }
}


In addition, the ant task also provides a class for other compressed formats, and interested students may wish to download an Apache Ant source code package for research.

Http://ant.apache.org/index.html
Good luck!!

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.