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!!