Sie müssen das Ant.jar -Paket importieren und auf der Apache -Website (http://ant.apache.org/bindownload.cgi) herunterladen.
Die Codekopie lautet wie folgt:
import Java.io.BufferedInputStream;
importieren java.io.bufferedOutputStream;
Import Java.io.file;
import Java.io.FileInputStream;
importieren java.io.fileoutputStream;
importieren 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;
öffentliche Klasse Ziputil {
private statische endgültige String-Encodes = "UTF-8";
public static void Zip (String InputFilePath, String Zipfilename) {
Datei inputFile = neue Datei (inputFilePath);
if (! inputFile.exists ())
Neue RunTimeException werfen ("Die Originaldatei existiert nicht !!!");
Datei BaseetarzipFile = neue Datei (Zipfilename) .GetParentFile ();
if (! baseetarzipfile.exists () &&! baseetarzipfile.mkdirs ())
Neue RunTimeException werfen ("Die Zieldatei kann nicht erstellt werden !!!");
BufferedOutputStream bos = null;
FileOutputStream out = null;
ZipoutputStream zout = null;
versuchen {
// Dateiausgabeobjekt erstellen, Eingabeaufforderung: Achten Sie auf den chinesischen Support
out = new FileOutputStream (neuer String (Zipfilename.getBytes (codieren)));
BOS = neuer BufferedOutputStream (out);
// Dateiausgabe -Streaming ist angeschlossen
Zout = neuer ZipoutputStream (BOS);
ZIP (ZIUT, InputFile, InputFile.getName ());
Closeioutil.closeAll (Zout, bos, out);
} catch (Ausnahme e) {
E. printstacktrace ();
}
}
private statische void ZIP (ZipoutputStream -Zout, Dateidatei, String -Basis) {
versuchen {
// Wenn das Dateihandle ein Verzeichnis ist
if (file.isdirectory ()) {
// Erhalten Sie die Datei im Verzeichnis
Datei [] listfiles = file.listfiles ();
// Erstellen Sie einen Zip -Eintrag
zout.putNextEntry (neu Zipentry (Basis + "/"));
Base = (Base.length () == 0? "": Base + "/");
if (listFiles! = null && listFiles.length> 0)
// Reisen Sie die Dateien im Verzeichnis auf
für (Datei F: Listfiles)
// Eingeben Sie diese Methode rekursiv ein
Zip (Zout, F, Base + F.getName ());
}
// Wenn das Dateihandle eine Datei ist
anders {
if (Base == "") {
Base = Datei.getName ();
}
// Füllen Sie das Dateihandle aus
zout.putNextEntry (neu Zipentry (Basis));
// Komprimierung starten
// Lesen Sie aus dem Dateizufluss, schreiben Sie in den Zip -Abfluss
WriteFile (Zout, Datei);
}
} catch (Ausnahme e) {
E. printstacktrace ();
}
}
private statische Leere Schreibfile (ZipoutputStream -Zout, Dateidatei)
löst ioException {aus
FileInputStream in = null;
BufferedInputStream Bis = null;
in = new FileInputStream (Datei);
Bis = neuer 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);
}
/***
* Dekompression
*
* @param zippath
* ZIP -Dateipfad
* @param destinationPath
* Dekompressionsziel
* @param ecode
* Codierter Zeichensatz von Dateinamen
*/
public static void Unzip (String Zippath, String ZielPath) {
Datei zipfile = neue Datei (Zippath);
if (! Zipfile.exists ())
Werfen Sie neue RunTimeException ("ZIP -Datei" + Zippath
+ "existiert nicht.");
Projekt proj = new Project ();
Expt expt = new expt ();
expt.setProject (proj);
expon.settaskType ("Unzip");
expand.settaskName ("unzip");
expt.setsrc (Zipfile);
expt.setDDest (neue Datei (ZielPath));
expt.setEncoding (codieren);
expin.execute ();
System.out.println ("Unzip Done !!!");
}
public static void main (String [] args) {
String dir = new String ("f: // mein backup // document // myeclipse+9.0 offizielle Version Crack und Aktivierung (professioneller Test verfügbar)");
Dir = new String ("f: /111.jpg");
ZIP (Dir, "f: /bzbxb/zipant.zip");
Unzip ("f: /bzbxb/zipant.zip", "f:/xx/xx/");
}
}