The method in ant can be used to implement compression and decompression of zip, including Chinese.
The specific implementation code is as follows:
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.logging.Level;
Import Java.util.logging.Logger;
Import Java.util.zip.ZipOutputStream;
Import Org.apache.tools.ant.Project;
Import Org.apache.tools.ant.taskdefs.Expand;
Import Org.apache.tools.zip.ZipEntry; /** * * @author Yaohucaizi/public class Ziputil {/** * * * @param file to compress files * @param zipfile compression
File Storage Place */public static void Zip (file file, file ZipFile) {Zipoutputstream outputstream = null;
try {outputstream = new Zipoutputstream (new FileOutputStream (ZipFile));
ZipFile (outputstream, File, "");
if (OutputStream!= null) {Outputstream.flush ();
Outputstream.close ();
} catch (IOException ex) {Logger.getlogger (ZipUtil.class.getName ()). log (Level.severe, NULL, ex);
Finally {try { Outputstream.close ();
The catch (IOException ex) {Logger.getlogger (ZipUtil.class.getName ()). log (Level.severe, NULL, ex); }}/** * * @param output Zipoutputstream Object * @param file or folder to compress * @param BasePath Entry root directory/private static void ZipFile (Zipoutputstream output, file file, String basepath) {Filei
Nputstream input = null; try {//file is directory if (File.isdirectory ()) {//Get the list of files in the current directory file Lis
T[] = File.listfiles (); BasePath = BasePath + (basepath.length () = = 0?
"": "/") + File.getname ();
Cyclic recursion compresses each file for (file f:list) {ZipFile (output, F, basepath); } else {//compressed file BasePath = (basepath.length () = 0? "": BasePath + "/") + File.getnamE ();
System.out.println (BasePath);
Output.putnextentry (New ZipEntry (BasePath));
input = new FileInputStream (file);
int readlen = 0;
byte[] buffer = new byte[1024 * 8];
while ((Readlen = input.read (buffer, 0, 1024 * 8))!=-1) {output.write (buffer, 0, Readlen);
A catch (Exception ex) {ex.printstacktrace ());
Finally {//close stream if (input!= null) {try {input.close (); The catch (IOException ex) {Logger.getlogger (ZipUtil.class.getName ()). log (Level.severe,
NULL, ex); /** * * * @param sourcezip to extract file path * @param destdir extract to Path * p ublic static void UnZip (String sourcezip, String destdir) {//Guaranteed folder path is Last "/" or "\" char Lastchar = dEstdir.charat (Destdir.length ()-1);
if (Lastchar!= '/' && Lastchar!= ' \) {destdir = File.separator;
Project P = new Project ();
Expand e = new Expand ();
E.setproject (P);
E.SETSRC (New File (Sourcezip));
E.setoverwrite (FALSE);
E.setdest (New File (Destdir));
/* Ant the Zip tool default compression encoding for UTF-8 encoding, while WinRAR software compression is used by Windows default GBK or GB2312 encoding so when decompression to develop coding format
* * e.setencoding ("GBK");
E.execute ();
public static void Main (string[] args) {String SourcePath = "C:/model.zip";
String destpath = "C:/test";
UnZip (SourcePath, DestPath);
Zip (new file ("C:/test/model"), New file ("D:/model.zip"));
}
}
The above code can realize zip file compression and decompression, this process required jar package Click link download Download link