คุณต้องนำเข้าแพ็คเกจ ant.jar และดาวน์โหลดบนเว็บไซต์ Apache (http://ant.apache.org/bindownload.cgi)
การคัดลอกรหัสมีดังนี้:
นำเข้า Java.io.BufferedInputStream;
นำเข้า Java.io.BufferedOutputStream;
นำเข้า Java.io.File;
นำเข้า Java.io.FileInputStream;
นำเข้า Java.io.FileOutputStream;
นำเข้า java.io.ioException;
นำเข้า java.util.zip.zipoutputStream;
นำเข้า org.apache.tools.ant.project;
นำเข้า org.apache.tools.ant.taskdefs.expand;
นำเข้า org.apache.tools.zip.zipentry;
นำเข้า com.xyq.io.util.closeioutil;
ชั้นเรียนสาธารณะ ziputil {
สตริงสุดท้ายคงที่ส่วนตัว encode = "UTF-8";
void zip public public (สตริง inputfilepath, zipfilename สตริง) {
ไฟล์ inputFile = ไฟล์ใหม่ (inputFilePath);
if (! inputfile.exists ())
โยน runtimeException ใหม่ ("ไฟล์ต้นฉบับไม่มีอยู่ !!!");
ไฟล์ basetarzipfile = ไฟล์ใหม่ (zipfilename) .getParentFile ();
if (! basetarzipfile.exists () &&! basetarzipfile.mkdirs ())
โยน runtimeException ใหม่ ("ไฟล์เป้าหมายไม่สามารถสร้างได้ !!!");
bufferedOutputStream bos = null;
fileOutputStream out = null;
zipoutputStream zout = null;
พยายาม {
// สร้างวัตถุเอาต์พุตไฟล์ออกแจ้ง: ให้ความสนใจกับการสนับสนุนของจีน
out = new fileOutputStream (สตริงใหม่ (zipfilename.getBytes (เข้ารหัส)));
BOS = ใหม่ bufferedOutputStream (out);
// การสตรีมการสตรีมเอาต์พุตไฟล์
zout = new ZipOutputStream (BOS);
zip (zout, inputfile, inputfile.getName ());
closeioutil.closeall (zout, bos, out);
} catch (Exception e) {
E.PrintStackTrace ();
-
-
Void Zip ส่วนตัวคงที่ (ZipOutputStream Zout, ไฟล์ไฟล์, ฐานสตริง) {
พยายาม {
// ถ้าไฟล์ที่จับเป็นไดเรกทอรี
if (file.isdirectory ()) {
// รับไฟล์ในไดเรกทอรี
ไฟล์ [] listfiles = file.listfiles ();
// สร้างรายการซิป
zout.putnextentry (zipentry ใหม่ (ฐาน + "/"));
base = (base.length () == 0? "": base + "/");
if (listfiles! = null && listfiles.length> 0)
// เดินทางไฟล์ในไดเรกทอรี
สำหรับ (ไฟล์ f: listfiles)
// ป้อนวิธีนี้ซ้ำ
zip (zout, f, base + f.getName ());
-
// ถ้าไฟล์ที่จับเป็นไฟล์
อื่น {
if (base == "") {
base = file.getName ();
-
// กรอกข้อมูลในไฟล์
zout.putnextentry (zipentry ใหม่ (ฐาน));
// เริ่มการบีบอัด
// อ่านจากการไหลเข้าของไฟล์เขียนถึง zip outflow
writefile (zout, ไฟล์);
-
} catch (Exception e) {
E.PrintStackTrace ();
-
-
Private Static Void WriteFile (ZipOutputStream Zout, ไฟล์ไฟล์)
โยน ioexception {
FileInputStream ใน = null;
bufferedInputStream bis = null;
ใน = ใหม่ FileInputStream (ไฟล์);
bis = ใหม่ bufferedInputStream (in);
int len = 0;
ไบต์ [] buff = ไบต์ใหม่ [2048];
ในขณะที่ ((len = bis.read (buff))! = -1)
zout.write (buff, 0, len);
zout.flush ();
closeioutil.closeall (bis, in);
-
-
* การบีบอัด
-
* @param zippath
* เส้นทางไฟล์ zip
* @param ปลายทาง
* ปลายทางการบีบอัด
* @param ecode
* ชุดอักขระที่เข้ารหัสของชื่อไฟล์
-
โมฆะคงที่สาธารณะ unzip (สตริง zippath, สตริง destination path) {
ไฟล์ zipfile = ไฟล์ใหม่ (zippath);
if (! zipfile.exists ())
โยน runtimeException ใหม่ ("ไฟล์ zip" + zippath
+ "ไม่มีอยู่จริง");
โครงการ PROJ = โครงการใหม่ ();
Expand Expand = New Expand ();
Expand.SetProject (PROJ);
Expand.SetTaskType ("UNZIP");
Expand.settaskname ("UNZIP");
Expand.setSrc (ZipFile);
Expand.setDest (ไฟล์ใหม่ (ปลายทาง path));
Expand.setEncoding (เข้ารหัส);
Expand.execute ();
System.out.println ("unzip เสร็จแล้ว !!!");
-
โมฆะคงที่สาธารณะหลัก (สตริง [] args) {
String dir = สตริงใหม่ ("f: // การสำรองข้อมูลของฉัน // document // myeclipse+9.0 การแตกร้าวและการเปิดใช้งานอย่างเป็นทางการ (มีการทดสอบระดับมืออาชีพ)");
dir = สตริงใหม่ ("f: /111.jpg");
zip (dir, "f: /bzbxb/zipant.zip");
unzip ("f: /bzbxb/zipant.zip", "f:/xx/xx/");
-
-