Java uses org.apache.tools.zip. * to compress/decompress the fileSource code
Java uses org.apache.tools.zip. * to compress/decompress the File SourceCode. The source code supports Chinese paths and Chinese file names; solved the bug that there is a folder with the same name after compression, and "\" before the file name in the root directory when the file is opened with 7-zip after compression.
Package COM. defonds. CDs. util; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; public class antziputil {public static void zipfile (string zipfilename, string inputfilename) throws exception {org.apache.tools.zip. zipoutputstream out = new org.apache.tools.zip. zipoutputstream (New fileoutputstream (zipfilename); out. s Etencoding ("GBK"); file inputfile = new file (inputfilename); zipit (Out, inputfile, "", true); out. close ();}/** supports the Chinese Compression parameter base starting from "" first to true */public static void zipit(org.apache.tools.zip. zipoutputstream out, file F, string base, Boolean first) throws exception {If (F. isdirectory () {file [] FL = f. listfiles (); If (first) {First = false;} else {base = base + "/";} For (INT I = 0; I <FL. length; I ++) {Zipit (Out, FL [I], base + FL [I]. getname (), first) ;}} else {If (first) {base = f. getname ();} Out. putnextentry (New org.apache.tools.zip. zipentry (base); fileinputstream in = new fileinputstream (f); int B; while (B = in. read ())! =-1) {out. write (B);} In. close () ;}} public static void unzipfile (string unzipfilename, string unzippath) throws exception handler org.apache.tools.zip. zipfile = new org.apache.tools.zip. zipfile (unzipfilename); unzipfilebyopache (zipfile, unzippath);}/** decompress the file unzip to the decompressed path */public static void unzipfilebyopache(org.apache.tools.zip. zipfile, string unziproot) throws exception, ioexception {Java. util. Enumeration E = zipfile.getentries(registry.org.apache.tools.zip. zipentry; while (E. hasmoreelements () {zipentry = (org.apache.tools.zip. zipentry) E. nextelement (); inputstream FCM = zipfile. getinputstream (zipentry); If (zipentry. isdirectory () {} else {file = new file (unziproot + file. separator + zipentry. getname (); file parentfile = file. getparentfile (); parentfile. mkdirs (); fileoutputstream fo S = new fileoutputstream (File); byte [] B = new byte [1024]; int Len; while (LEN = Fi. read (B, 0, B. length ))! =-1) {FOS. write (B, 0, Len);} FOS. close (); FCM. close () ;}} public static void main (string [] ARGs) throws exception {zipfile ("D:/temp/folders.zip", "D: /temp/Folders "); // compression entry // unzipfile (" D:/javafile.zip "," E:/"); // decompress the entry }}