Artikel ini menjelaskan kompresi ZIP dan kelas alat dekompresi yang diimplementasikan di Java. Bagikan untuk referensi Anda, sebagai berikut:
impor java.io.bufferedInputStream; impor java.io.bufferedoutputstream; impor java.io.file; impor java.io.fileinputStream; impor java.io.filenotfoundException; import java.fileoutputstream; impor java.io.ioEcception; java.util.enumeration; impor org.apache.tools.zip.zipentry; impor org.apache.tools.zip.zipfile; impor org.apache.tools.zip.zipoutputStream; kelas publik ziputil {private static int bufferedSize = 1024; / ** * File terkompresi * * @param zipfilename * Path file paket terkompresi yang disimpan * @param filepath * folder atau jalur file yang perlu dikompresi * @param isDelete * apakah akan menghapus file sumber * @THROWS */ public void zip (string zipfileName, string isdePath, booleRenam (boolePel neolePel (string zipfilename, string isdePath, booleRename (boolePel neolePel (string zipfilename, string isDePath, booleREnt noleNAME (string zipfilename, string isDePath, booleREneMel File (filepath), isDelete); } /** * Compressed file* * @param zipFileName * Saved compressed package file path* @param inputFile * Folder or file that needs to be compressed* @param isDelete * Whether to delete the source file* @throws Exception */ public void zip(String zipFileName, File inputFile, boolean isDelete) throws Exception { ZipOutputStream out = new ZipOutputStream (FileOutputStream baru (ZipFileName)); if (! inputFile.exists ()) {lempar FileNotFoundException baru ("Tidak ada file yang akan dikompresi ditemukan di jalur yang ditentukan!"); } zip (out, inputFile, "", isDelete); out.close (); } /** * Recursive compression method* * @param out * Compressed package output stream* @param f * File to be compressed* @param base * Compressed path* @param isDelete * Whether to delete the source file* @throws Exception */ private void zip(ZipOutputStream out, File inputFile, String base, boolean isDelete) throws Exception { if (inputFile.isDirectory ()) {// Jika itu adalah file direktori [] inputFiles = inputFile.listFiles (); out.putNextEntry (zipentry baru (base + "/")); base = base.length () == 0? "": base + "/"; untuk (int i = 0; i <inputFiles.length; i ++) {zip (out, inputFiles [i], base+inputFiles [i] .getName (), isDelete); }} else {// Jika itu adalah file if (base.length ()> 0) {out.putNextEntry (zipEntry baru (base)); } else {out.putNextEntry (zipEntry baru (inputFile.getName ())); } FileInputStream di = FileInputStream baru (inputFile); coba {int len; byte [] buff = byte baru [bufferedSize]; while ((len = in.read (buff))! = -1) {out.write (buff, 0, len); }} catch (ioException e) {throw e; } akhirnya {in.close (); }} if (isDelete) {inputFile.delete (); }} / ** * decompress * * @param zipfilePath * Jalur paket kompres * @param filesavepath * lintasan unzip * @param isDelete * apakah akan menghapus file sumber * @Throws Exception * / public void unzip (string zipfilePath, string fileseveAvePath, boolean isDelete) File (FileSavEpath)). Mkdirs (); File f = file baru (zipfilePath); if ((! f.exists ()) && (f.length () <= 0)) {lempar pengecualian baru ("File yang akan didekompresi tidak ada!"); } Zipfile zipfile = zipfile baru (f); String Strpath, GBKPath, Strtemp; File tempfile = file baru (FileSavEpath); // Mulai dari direktori saat ini strpath = tempfile.getAbsolutePath (); // Posisi absolut dari enumerasi output <ImlipEntry> e = zipfile.getentries (); while (e.hasmoreElements ()) {org.apache.tools.zip.zipentry zipent = E.NextElement (); gbkpath = zipent.getName (); if (zipent.isdirectory ()) {strtemp = strpath + file.separator + gbkpath; File dir = file baru (strtemp); dir.mkdirs (); melanjutkan; } else {// baca dan tulis file inputStream adalah = zipfile.getInputStream (zipent); BufferedInputStream bis = baru bufferedInputStream (IS); gbkpath = zipent.getName (); strtemp = strpath + file.separator + gbkpath; // Buat direktori string strsubdir = gbkpath; untuk (int i = 0; i <strsubdir.length (); i ++) {if (strsubdir.substring (i, i + 1) .equalSignorecase ("/")) {string temp = strpath + file.separator + strsubdir.substring (0, i); File subdir = file baru (temp); if (! Subdir.exists ()) subdir.mkdir (); }} FileOutputStream fos = FileOutputStream baru (strtemp); BufferedOutputStream bos = baru bufferedoutputStream (fos); int len; byte [] buff = byte baru [bufferedSize]; while ((len = bis.read (buff))! = -1) {bos.write (buff, 0, len); } bos.close (); fos.close (); }}} catch (Exception e) {e.printstacktrace (); lempar e; } if (isDelete) {file baru (zipfilePath) .delete (); }} // public static void main (string [] args) {// ziputil cpr = new ziputil (); // coba {// cpr.zip ("c:/user/lenovo pengguna/desktop/uji cina. cpr.unzip ("c:/user/lenovo pengguna/desktop/uji cina.zip", "c:/user/lenovo pengguna/desktop/folder baru 2", false); //} catch (pengecualian e) {// e.printstacktrace (); //} /////////////////// // {// e.printstacktrace ();Untuk informasi lebih lanjut tentang algoritma Java, pembaca yang tertarik dengan situs ini dapat melihat topik: "Ringkasan File Java dan Keterampilan Operasi Direktori", "Tutorial tentang Struktur Data Java dan Algoritma", "Ringkasan Keterampilan Operasi Java Dom" dan "Ringkasan Keterampilan Operasi Java Cache"
Saya harap artikel ini akan membantu pemrograman Java semua orang.