คลาสเครื่องมือการบีบอัดซิปแบบง่าย ๆ ที่เขียนโดยใช้ Java Basic Class
การคัดลอกรหัสมีดังนี้:
Package Sun.net.helper;
นำเข้า Java.io.*;
นำเข้า java.util.logging.logger;
นำเข้า java.util.zip.*;
ชั้นเรียนสาธารณะ ziputil {
Logger Logger คงที่ส่วนตัวสุดท้าย = logger.getLogger (ziputil.class.getName ());
บัฟเฟอร์ int สุดท้ายคงที่ส่วนตัว = 1024*10;
-
* บีบอัดไดเรกทอรีที่ระบุไปยังไฟล์ zip ที่มีชื่อเดียวกับไดเรกทอรีและปรับแต่งเส้นทางการบีบอัด
* @Param SourceFilePath Target File Path
* @param zipfilepath ระบุเส้นทางไฟล์ zip
* @กลับ
-
STATIC BOOLEAN STATIC STATIC (String SourceFilePath, String ZipFilePath)
ผลลัพธ์บูลีน = false;
File Source = ไฟล์ใหม่ (SourceFilePath);
if (! source.exists ()) {
logger.info (SourceFilePath+"ไม่มีอยู่");
ผลการกลับมา;
-
if (! source.isdirectory ()) {
logger.info (SourceFilePath+"ไม่ใช่ไดเรกทอรี");
ผลการกลับมา;
-
ไฟล์ zipfile = ไฟล์ใหม่ (zipfilepath+"/"+source.getName ()+". zip");
if (zipfile.exists ()) {
logger.info (zipfile.getName ()+"มีอยู่แล้ว");
ผลการกลับมา;
}อื่น{
if (! zipfile.getParentFile (). มีอยู่ ()) {
if (! zipfile.getParentFile (). mkdirs ()) {
logger.info ("ไม่สามารถสร้างไฟล์"+zipfile.getName ());
ผลการกลับมา;
-
-
-
logger.info ("การสร้างไฟล์ zip ... ");
fileOutputStream dest = null;
zipoutputStream out = null;
พยายาม {
dest = new fileOutputStream (zipfile);
checkedOutputStream checksum = new CheckedOutputStream (DEST, ADLER32 () ใหม่);
out = new ZipOutputStream (ใหม่ bufferedOutputStream (checksum));
out.setMethod (ZipOutputStream.deflated);
บีบอัด (แหล่งที่มา, out, source.getName ());
ผลลัพธ์ = จริง;
} catch (filenotfoundexception e) {
E.PrintStackTrace ();
} ในที่สุด {
ถ้า (ออก! = null) {
พยายาม {
out.closeentry ();
} catch (ioexception e) {
E.PrintStackTrace ();
-
พยายาม {
out.close ();
} catch (ioexception e) {
E.PrintStackTrace ();
-
-
-
ถ้า (ผลลัพธ์) {
logger.info ("เสร็จแล้ว");
}อื่น{
logger.info ("ล้มเหลว");
-
ผลการกลับมา;
-
การบีบอัดโมฆะแบบคงที่ส่วนตัว (ไฟล์ไฟล์, ZipOutputStream Out, String MainFilename) {
if (file.isfile ()) {
FileInputStream fi = null;
BufferedInputStream Origin = NULL;
พยายาม {
fi = ใหม่ fileInputStream (ไฟล์);
Origin = New BufferedInputStream (fi, buffer);
int index = file.getabsolutepath (). indexof (mainfilename);
string entryName = file.getAbsolutePath (). substring (ดัชนี);
System.out.println (entryName);
zipentry entry = new Zipentry (entryName);
out.putnextentry (รายการ);
ไบต์ [] data = ไบต์ใหม่ [บัฟเฟอร์];
จำนวน int;
ในขณะที่ ((count = origin.read (data, 0, buffer))! = -1) {
out.write (data, 0, count);
-
} catch (filenotfoundexception e) {
E.PrintStackTrace ();
} catch (ioexception e) {
E.PrintStackTrace ();
} ในที่สุด {
if (Origin! = null) {
พยายาม {
Origin.close ();
} catch (ioexception e) {
E.PrintStackTrace ();
-
-
-
} อื่นถ้า (file.isdirectory ()) {
ไฟล์ [] fs = file.listfiles ();
if (fs! = null && fs.length> 0) {
สำหรับ (ไฟล์ f: fs) {
ประคบ (f, out, mainfilename);
-
-
-
-
-
* คลายซิปไฟล์ zip ไปยังไดเรกทอรีที่ระบุ
* @param zipfile
* @param destpath
* @กลับ
-
Public Static Boolean Unzip (ไฟล์ zipfile, String destpath) {
ผลลัพธ์บูลีน = false;
if (! zipfile.exists ()) {
logger.info (zipfile.getName ()+"ไม่มีอยู่");
ผลการกลับมา;
-
file target = ไฟล์ใหม่ (destpath);
if (! target.exists ()) {
if (! target.mkdirs ()) {
logger.info ("ไม่สามารถสร้างไฟล์"+target.getName ());
ผลการกลับมา;
-
-
String mainFilename = zipfile.getName (). แทนที่ (". zip", "");
ไฟล์ targetFile = ไฟล์ใหม่ (destpath+"/"+mainfilename);
if (targetFile.exists ()) {
logger.info (targetFile.getName ()+"มีอยู่แล้ว");
ผลการกลับมา;
-
zipinputStream zis = null;
logger.info ("เริ่มไฟล์ unzip ... ");
พยายาม {
FileInputStream FIS = ใหม่ FileInputStream (ZipFile);
checkedInputStream checksum = new CheckedInputStream (FIS, ADLER32 () ใหม่);
zis = ใหม่ zipinputstream (ใหม่ bufferedInputStream (checksum));
รายการ Zipentry;
ในขณะที่ ((entry = zis.getnextentry ())! = null) {
จำนวน int;
ข้อมูลไบต์ [] = ไบต์ใหม่ [บัฟเฟอร์];
string entryName = entry.getName ();
int index = entryName.indexof (mainfilename);
String newEntryName = destPath+"/"+entryName.SubString (ดัชนี);
System.out.println (ชื่อใหม่);
File Temp = ไฟล์ใหม่ (newEntryName) .getParentFile ();
if (! temp.exists ()) {
if (! temp.mkdirs ()) {
โยน runtimeException ใหม่ ("สร้างไฟล์" +temp.getName () +"ล้มเหลว");
-
-
fileOutputStream fos = new fileOutputStream (newEntryName);
bufferedOutputStream dest = new bufferedOutputStream (fos, buffer);
ในขณะที่ ((count = zis.read (data, 0, buffer))! = -1) {
dest.write (data, 0, count);
-
dest.flush ();
dest.close ();
-
ผลลัพธ์ = จริง;
} catch (filenotfoundexception e) {
E.PrintStackTrace ();
} catch (ioexception e) {
E.PrintStackTrace ();
} ในที่สุด {
ถ้า (zis! = null) {
พยายาม {
zis.close ();
} catch (ioexception e) {
E.PrintStackTrace ();
-
-
-
ถ้า (ผลลัพธ์) {
logger.info ("เสร็จแล้ว");
}อื่น{
logger.info ("ล้มเหลว");
-
ผลการกลับมา;
-
โมฆะคงที่สาธารณะหลัก (String [] args) พ่น IOException {
//ziputil.zip("d:/apache-tomcat-7.0.30 "," d:/temp ");
ไฟล์ zipfile = ไฟล์ใหม่ ("d: /temp/apache-tomcat-7.0.30.zip");
ziputil.unzip (zipfile, "d:/temp");
-
-
ตัวอย่างการบีบอัดและการบีบอัดอีกตัวอย่างโปรดดูเครื่องมือสองอย่าง
การคัดลอกรหัสมีดังนี้:
แพ็คเกจ com.lanp;
นำเข้า Java.io.File;
นำเข้า Java.io.FileInputStream;
นำเข้า Java.io.FileOutputStream;
นำเข้า java.io.ioException;
นำเข้า java.util.zip.zipentry;
นำเข้า java.util.zip.zipexception;
นำเข้า java.util.zip.zipfile;
นำเข้า java.util.zip.zipinputstream;
-
* คลายซิปไฟล์บีบอัดซิปไปยังไดเรกทอรีที่ระบุ
-
ระดับสุดท้ายของคลาสสาธารณะ ziptofile {
-
* ขนาดแคชเริ่มต้นเป็น 20480
-
INT คงที่ส่วนตัวสุดท้าย int file_buffer_size = 20480;
ziptofile ส่วนตัว () {
-
-
* คลายซิปไฟล์บีบอัดซิปของไดเรกทอรีที่ระบุไปยังไดเรกทอรีที่ระบุ
* @param zipfilepathzip เส้นทางไฟล์บีบอัด
* @param zipfilenamezip ชื่อไฟล์บีบอัด
* @param targetfiledirzip ไฟล์บีบอัดที่จะสกัด
* @return Flag Boolean ค่าคืนค่า
-
Public Static Boolean Unzip (String ZipFilePath, String ZipFileName, String TargetFiledir) {
ธงบูลีน = เท็จ;
// 1. กำหนดว่าไฟล์ที่ถูกบีบอัดมีอยู่หรือไม่
ไฟล์ไฟล์ = null; // ไฟล์บีบอัด (พร้อมพา ธ )
zipfile zipfile = null;
file = ไฟล์ใหม่ (zipfilepath + "/" + zipfilename);
System.out.println (">>>>>>> คลายซิปไฟล์ [" + zipfilepath + "/" + zipfilename + "] ไปยังไดเรกทอรี [" + targetfiledir + "] <<<<<<<<<<
if (false == file.exists ()) {
System.out.println (">>>>>>> ไฟล์บีบอัด [" + zipfilepath + "/" + zipfilename + "] ไม่มีอยู่ <<<<<<<");
กลับเท็จ;
} อื่นถ้า (0 == file.length ()) {
System.out.println (">>>>>>> ไฟล์บีบอัด [" + zipfilepath + "/" + zipfilename + "] ขนาดคือ 0 และไม่จำเป็นต้องมีการบีบอัด <<<<<<<");
กลับเท็จ;
} อื่น {
// 2.
ไบต์ [] buf = ไบต์ใหม่ [file_buffer_size];
int readsize = -1;
zipinputStream zis = null;
fileOutputStream fos = null;
พยายาม {
// ตรวจสอบว่าเป็นไฟล์ zip หรือไม่
zipfile = zipfile ใหม่ (ไฟล์);
zipfile.close ();
// พิจารณาว่าไดเรกทอรีเป้าหมายมีอยู่หรือไม่
ไฟล์ newDir = ไฟล์ใหม่ (TargetFiledir);
if (false == newdir.exists ()) {
newdir.mkdirs ();
newdir = null;
-
zis = new ZipInputStream (ใหม่ FileInputStream (ไฟล์));
zipentry zipentry = zis.getNextentry ();
// เริ่มการประมวลผลไฟล์ในแพ็คเกจบีบอัด
ในขณะที่ (null! = zipentry) {
string zipentryName = zipentry.getName (). แทนที่ ('//', '/');
// ตัดสินว่า Zipentry เป็นไดเรกทอรีหรือไม่
if (zipentry.isdirectory ()) {
int indexnumber = zipentryName.lastindexof ('/');
ไฟล์ entryDirs = ไฟล์ใหม่ (targetFiledir + "/" + zipentryName.substring (0, indexNumber));
entrydirs.mkdirs ();
entryDirs = null;
} อื่น {
พยายาม {
FOS = ใหม่ fileOutputStream (TargetFiledir + "/" + zipentryName);
ในขณะที่ ((readsize = zis.read (buf, 0, file_buffer_size))! = -1) {
fos.write (buf, 0, readsize);
-
} catch (Exception e) {
E.PrintStackTrace ();
โยน runtimeException ใหม่ (e.getcause ());
} ในที่สุด {
พยายาม {
ถ้า (null! = fos) {
fos.close ();
-
} catch (ioexception e) {
E.PrintStackTrace ();
โยน runtimeException ใหม่ (e.getcause ());
-
-
-
zipentry = zis.getNextentry ();
-
ธง = จริง;
} catch (zipexception e) {
E.PrintStackTrace ();
โยน runtimeException ใหม่ (e.getcause ());
} catch (ioexception e) {
E.PrintStackTrace ();
โยน runtimeException ใหม่ (e.getcause ());
} ในที่สุด {
พยายาม {
ถ้า (null! = zis) {
zis.close ();
-
ถ้า (null! = fos) {
fos.close ();
-
} catch (ioexception e) {
E.PrintStackTrace ();
โยน runtimeException ใหม่ (e.getcause ());
-
-
-
ธงกลับ;
-
-
* วิธีการหลักสำหรับการทดสอบ
-
โมฆะคงที่สาธารณะหลัก (สตริง [] args) {
string zipfilepath = "c: // home";
string zipfilename = "lp20120301.zip";
สตริง targetFiledir = "c: // home // lp20120301";
Boolean Flag = ziptofile.unzip (zipfilepath, zipfilename, targetfiledir);
ถ้า (ธง) {
System.out.println (">>>>>>> คลายการบีบอัดสำเร็จ <<<<<<");
} อื่น {
System.out.println (">>>>>>> การบีบอัดล้มเหลว <<<<<<");
-
-
-