ในระหว่างกระบวนการถ่ายโอนไฟล์เพื่อให้ไฟล์ขนาดใหญ่สะดวกและส่งสัญญาณเร็วขึ้นโดยทั่วไปการบีบอัดจะใช้เพื่อบีบอัดไฟล์ก่อนที่จะถ่ายโอน คลาส deflater และ inflater ในแพ็คเกจ java.util.zip ใน Java ให้ฟังก์ชั่นการบีบอัดของอัลกอริทึม deflate แก่ผู้ใช้ ต่อไปนี้คือการบีบอัดและการบีบอัดการบีบอัดที่เขียนด้วยตัวเองและเนื้อหาไฟล์บีบอัดจะแสดงเป็นตัวอย่าง วิธีการเฉพาะที่เกี่ยวข้องสามารถดูได้ใน JDK API สำหรับข้อมูล
/ ** * * @param inputByte * byte array ที่จะคลายการบีบอัด * @return decompressed byte array * @throws ioexception */ public static byte [] unpress (byte [] inputByte) โยน ioexception {int len = 0; inflater infl = new inflater (); infl.setInput (inputByte); ByTeArrayOutputStream BOS = New ByTeArrayOutputStream (); ไบต์ [] Outbyte = byte ใหม่ [1024]; ลอง {ในขณะที่ (! infl.finished ()) {// decompress และส่งออกเนื้อหาที่บีบอัดไปยังสตรีมเอาต์พุตไบต์ bos len = infl.inflate (outbyte); if (len == 0) {break; } bos.write (outbyte, 0, len); } infl.end (); } catch (exception e) {//} ในที่สุด {bos.close (); } return bos.tobytearray (); } /*** บีบอัด * * @param InputByte * อาร์เรย์ไบต์ที่จะบีบอัด * @return ข้อมูลบีบอัด * @throws ioexception */ สาธารณะคงที่สาธารณะ [] บีบอัด (ไบต์ [] inputByte) โยน ioexception {int len = 0; deflater defl = new deflater (); defl.setInput (inputByte); defl.finish (); ByTeArrayOutputStream BOS = New ByTeArrayOutputStream (); ไบต์ [] outputByte = ไบต์ใหม่ [1024]; ลอง {ในขณะที่ (! defl.finished ()) {// บีบอัดและส่งออกเนื้อหาที่ถูกบีบอัดไปยังสตรีมเอาต์พุต BYTE bos len = defl.deflate (outputByte); bos.write (outputbyte, 0, len); } defl.end (); } ในที่สุด {bos.close (); } return bos.tobytearray (); } โมฆะคงที่สาธารณะหลัก (สตริง [] args) {ลอง {fileInputStream fis = ใหม่ fileInputStream ("d: //testdeflate.txt"); int len = fis.available (); ไบต์ [] b = ไบต์ใหม่ [len]; fis.read (b); ไบต์ [] bd = บีบอัด (b); // เพื่อให้เนื้อหาที่ถูกบีบอัดถูกส่งบนเครือข่ายโดยทั่วไปสตริงการเข้ารหัส BASE64 BASE64 จะใช้เพื่อเข้ารหัส encodestr = base64.encodeBase64String (BD); Byte [] bi = unpress (base64.decodeBase64 (encodestr)); fileOutputStream fos = ใหม่ fileOutputStream ("d: //testinflate.txt"); fos.write (bi); fos.flush (); fos.close (); fis.close (); } catch (Exception E) {//}}วิธีการใช้งานการบีบอัดแบบ deflate ข้างต้นใน Java เป็นเนื้อหาทั้งหมดที่ฉันได้แบ่งปันกับคุณ ฉันหวังว่าคุณจะให้ข้อมูลอ้างอิงและฉันหวังว่าคุณจะสนับสนุน wulin.com มากขึ้น