Java uses org.apache.tools.zip. * to compress the file source code

Source: Internet
Author: User

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 }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.