ตัวอย่างนี้ใช้ zip zip input และ output stream ของ Java เพื่อบีบอัดและคลายไฟล์ ส่วนก่อนหน้าของรหัสใช้การได้มาของเส้นทางไฟล์การเปลี่ยนแปลงชื่อไฟล์ที่บีบอัด ฯลฯ ดังนี้:
แพ็คเกจ com.utility.zip; นำเข้า java.io.bufferedInputStream; นำเข้า java.io.bufferedOutputStream; นำเข้า java.io.file; นำเข้า java.io.fileinputstream นำเข้า java.io.fileoutputstream; liujiduo * */public final class ziputil {private ziputil () {// ว่างเปล่า}/** * ไฟล์บีบอัดไฟล์ * * @param filepath * ไฟล์ไฟล์ filepath = ไฟล์ filepath (source.exists ()) {// ชื่อไฟล์ที่บีบอัด = source filename.zipstring zipname = source.getName () + ".zip"; target = ไฟล์ใหม่ (source.getParent (), zipname); if (target.exists ()) {target.delete () {fos = new fileOutputStream (เป้าหมาย); zos = new ZipOutputStream (ใหม่ bufferedOutputStream (fos)); // เพิ่มไฟล์ enterteddentry ("/", zos);} catch (ioexception e) Target;}/** * สแกนเพื่อเพิ่มรายการไฟล์ * * @param base * path ฐาน * @param แหล่งที่มา * ไฟล์แหล่งที่มา * @param zos * zip output stream * @throws ioexception */private static void addentry (สตริงฐาน, ไฟล์, zipootputstream zos) source.getName (); if (source.isdirectory ()) {สำหรับ (ไฟล์ไฟล์: source.listfiles ()) {// แสดงรายการไฟล์ทั้งหมดในไดเรกทอรีและเพิ่มไฟล์ entryaddentry (รายการ + "/", zos); * 10]; fis = ใหม่ fileInputStream (แหล่งที่มา); bis = ใหม่ bufferedInputStream (fis, buffer.length); int read = 0; zos.putNextentry (zipentry ใหม่ (รายการ)); ในขณะที่ ((read = bis.read (บัฟเฟอร์, 0, buffer.length))! = -1) {zos.write (บัฟเฟอร์, 0, อ่าน);} zos.closeentry ();} ในที่สุด {ioutil.closequietly (bis, fis); filePath) {file source = ไฟล์ใหม่ (filePath); if (source.exists ()) {zipinputStream zis = null; bufferedOutputStream bos = null; ลอง {zis = ใหม่ zipinputStream (ใหม่ fileInputStream (แหล่งที่มา)); zipentry entry = null; entry.getName ()); if (! target.getParentFile (). มีอยู่ ()) {// สร้างไดเรกทอรีหลักของไฟล์ target.getParentFile (). mkdirs ();} // เขียนไปยังไฟล์ bos = new bufferedputStream zis.read (บัฟเฟอร์, 0, buffer.length))! = -1) {bos.write (บัฟเฟอร์, 0, อ่าน);} bos.flush ();} zis.closeentry ();} catch (ioexception e) {โยน runtimeException ใหม่ (e); Main (String [] args) {String targetPath = "e: // win7 วอลล์เปเปอร์"; ไฟล์ไฟล์ = ziputil.zip (TargetPath); system.out.println (ไฟล์); ziputil.unzip ("f: // win7 wallpaper.zip");}}}}}}}}}}ต่อไปนี้เป็นคำอธิบายภาษา Java สำหรับการปิดวัตถุสตรีมอย่างน้อยหนึ่งรายการผ่านคลาสเครื่องมือสตรีม IO เพื่อรับรายการวัตถุสตรีมปิดดังต่อไปนี้:
แพ็คเกจ com.utility.io; นำเข้า java.io.closeable; นำเข้า Java.io.ioException;/** * io Stream Tool Class * * @author liujiduo * */คลาสสาธารณะ ioutil {/** * ปิดการใช้งานอย่างน้อยหนึ่งวัตถุ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * {ถ้า (closeables! = null) {สำหรับ (ปิดได้ (ปิดได้: closeables) {ถ้า (ปิดได้! = null) {closedable.close ();}}}}}/*** ปิดวัตถุสตรีมหนึ่งหรือมากกว่าหนึ่งรายการ {Close (Closeables);} catch (ioexception e) {// ไม่ทำอะไรเลย}}}สรุป
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้เกี่ยวกับตัวอย่างรหัสของวิธีการใช้เครื่องมือไฟล์ Ziputil ที่บีบอัด Java ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน เพื่อนที่สนใจสามารถอ้างถึงหัวข้ออื่น ๆ ที่เกี่ยวข้องในเว็บไซต์นี้ต่อไป หากมีข้อบกพร่องใด ๆ โปรดฝากข้อความไว้เพื่อชี้ให้เห็น