Need to import Ant.jar package, Apache website (http://ant.apache.org/bindownload.cgi) can download.
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.zip.ZipOutputStream;
Import Org.apache.tools.ant.Project;
Import Org.apache.tools.ant.taskdefs.Expand;
Import Org.apache.tools.zip.ZipEntry;
Import Com.xyq.io.util.CloseIoUtil;
public class Ziputil {
private static final String ENCODE = "UTF-8";
public static void Zip (String inputfilepath, String zipfilename) {
file inputfile = new File (Inputfilepath);
if (!inputfile.exists ())
throw New RuntimeException ("Original file does not exist!!!");
file basetarzipfile = new File (zipfilename). Getparentfile ();
if (!basetarzipfile.exists () &&!basetarzipfile.mkdirs ())
throw New RuntimeException ("Target file cannot be created!!!");
bufferedoutputstream bos = NULL;
fileoutputstream out = null;
zipoutputstream zout = null;
try {
//Create a file output object out, hint: note Chinese support
out = new FileOutputStream (New String (Zipfilename.getbytes (ENCODE));
bos = new Bufferedoutputstream (out);
//The file out of the zip output stream
zout = new Zipoutputstream (BOS);
zip (Zout, Inputfile, Inputfile.getname ());
closeioutil.closeall (Zout, Bos, out);
catch (Exception e) {
E.printstacktrace ();
}
}
private static void Zip (Zipoutputstream zout, file file, String base) {
try {
If the file handle is a directory
if (File.isdirectory ()) {
Get the files in the directory
file[] Listfiles = File.listfiles ();
Create a Zip entry
Zout.putnextentry (new ZipEntry (base + "/"));
Base = (Base.length () = = 0? "": Base + "/");
if (listfiles!= null && listfiles.length > 0)
Traverse directory to the file
for (File f:listfiles)
Recursive access to this method
Zip (Zout, F, base + f.getname ());
}
If the file handle is a file
else {
if (base = = "") {
Base = File.getname ();
}
Fill in file handle
Zout.putnextentry (new ZipEntry (base));
Start compression
Read from file inflow, write zip out stream
WriteFile (zout, file);
}
catch (Exception e) {
E.printstacktrace ();
}
}
private static void WriteFile (Zipoutputstream zout, file file)
Throws IOException {
FileInputStream in = null;
Bufferedinputstream bis = null;
in = new FileInputStream (file);
bis = new Bufferedinputstream (in);
int len = 0;
byte[] buff = new byte[2048];
while (len = bis.read (Buff))!=-1)
Zout.write (buff, 0, Len);
Zout.flush ();
Closeioutil.closeall (bis, in);
}
/****
* Decompression
*
* @param Zippath
* Zip file path
* @param DestinationPath
* The location of the decompression destination
* @param ecode
* Coded character set for file name
*/
public static void UnZip (String zippath, String destinationpath) {
File ZipFile = new file (Zippath);
if (!zipfile.exists ())
throw new RuntimeException ("Zip file" + Zippath
+ "does not exist.");
Project proj = new Project ();
Expand Expand = new Expand ();
Expand.setproject (proj);
Expand.settasktype ("unzip");
Expand.settaskname ("unzip");
EXPAND.SETSRC (ZipFile);
Expand.setdest (New File (DestinationPath));
Expand.setencoding (ENCODE);
Expand.execute ();
System.out.println ("Unzip done!!!");
}
public static void Main (string[] args) {
String dir = new String ("f:\\ my backup \ Document \\myeclipse+9.0 official version cracked and activated (pro-Test available)");
dir = new String ("f:/111.jpg");
Zip (dir, "F:/bzbxb/zipant.zip");
UnZip ("F:/bzbxb/zipant.zip", "f:/xx/xx/");
}
}