تحتاج إلى استيراد حزمة 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" ؛
zip zip static static static (سلسلة inputFilePath ، سلسلة zipfilename) {
ملف inputFile = ملف جديد (inputFilePath) ؛
if (! inputFile.Exists ())
رمي new RunTimeException ("الملف الأصلي غير موجود !!!") ؛
file basetarzipfile = ملف جديد (zipfilename) .getParentFile () ؛
if (! basetarzipfile.exists () &&! basetarzipfile.mkdirs ())
رمي جديد RunTimeException ("لا يمكن إنشاء الملف الهدف !!!") ؛
bufferedoutputstream bos = null ؛
fileOutputStream Out = null ؛
zipoutputstream zout = null ؛
يحاول {
// إنشاء كائن إخراج الملف ، موجه: انتبه إلى الدعم الصيني
out = new FileOutputStream (سلسلة جديدة (zipfilename.getBytes (encode)))) ؛
BOS = جديد bufferedOutputStream (Out) ؛
// تم توصيل تدفق إخراج الملف
Zout = new ZipoutputStream (BOS) ؛
ZIP (Zout ، inputFile ، inputFile.getName ()) ؛
CloseIoutil.closeall (Zout ، BOS ، Out) ؛
} catch (استثناء e) {
E.PrintStackTrace () ؛
}
}
zip zip static static zipoutputstream ، ملف الملف ، قاعدة السلسلة) {
يحاول {
// إذا كان مقبض الملف هو دليل
if (file.isdirectory ()) {
// احصل على الملف في الدليل
file [] listfiles = file.listfiles () ؛
// إنشاء إدخال zip
zout.putnextentry (zipentry new (base + "/")) ؛
base = (base.length () == 0؟ "": base + "/") ؛
if (listfiles! = null && listfiles.length> 0)
// السفر الملفات في الدليل
لـ (File F: ListFiles)
// أدخل هذه الطريقة بشكل متكرر
Zip (zout ، f ، base + f.getName ()) ؛
}
// إذا كان مقبض الملف هو ملف
آخر {
if (base == "") {
base = file.getName () ؛
}
// املأ مقبض الملف
zout.putnextentry (new zipentry (base)) ؛
// ابدأ الضغط
// اقرأ من تدفق الملف ، اكتب إلى zip outflow
WriteFile (Zout ، ملف) ؛
}
} catch (استثناء e) {
E.PrintStackTrace () ؛
}
}
private static void writefile (zipoutputstream zout ، ملف ملف)
يلقي ioexception {
FileInputStream في = null ؛
BufferedInputStream BIS = NULL ؛
في = جديد fileInputStream (ملف) ؛
BIS = جديد bufferedInputStream (in) ؛
int len = 0 ؛
Byte [] Buff = New Byte [2048] ؛
بينما ((len = bis.read (buff))! = -1)
zout.write (Buff ، 0 ، len) ؛
zout.flush () ؛
CloseIoutil.closeall (مكرر ، في) ؛
}
/***
* تخفيف الضغط
*
* param Zippath
* مسار ملف zip
* param DestinationPath
* وجهة الضغط
* param ecode
* مجموعة الأحرف المشفرة من أسماء الملفات
*/
الفراغ الثابت العام unsip (سلسلة Zippath ، String DestinationPath) {
ملف zipfile = ملف جديد (Zippath) ؛
إذا (! zipfile.exists ())
رمي جديد RunTimeException ("ملف zip" + Zippath
+ "غير موجود.") ؛
Project proj = New Project () ؛
توسيع توسيع = جديد توسيع () ؛
exped.setProject (proj) ؛
sext.settasktype ("unsxip") ؛
sext.settaskName ("unsxip") ؛
sext.setsrc (zipfile) ؛
exped.setDest (ملف جديد (DestinationPath)) ؛
exped.setEncoding (encode) ؛
exped.execute () ؛
system.out.println ("unsxip done !!!") ؛
}
الفراغ الثابت العام الرئيسي (سلسلة [] args) {
String Dir = New String ("F: // My Backup // document // myeclipse+9.0 الإصدار الرسمي للتصدع والتفعيل (اختبار احترافي متاح)") ؛
dir = new string ("f: /111.jpg") ؛
zip (dir ، "f: /bzbxb/zipant.zip") ؛
unsip ("f: /bzbxb/zipant.zip" ، "f:/xx/xx/") ؛
}
}