Compress and decompress the folder file to solve Chinese garbled characters.

Source: Internet
Author: User

 

Package COM. scout. util; </P> <p> Import Java. io. bufferedinputstream; <br/> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. inputstream; <br/> Import Java. util. enumeration; </P> <p> Import org.apache.tools.zip. zipentry; <br/> Import org.apache.tools.zip. zipfile; <br/> Import org.apache.tools.zip. zipoutputstream; </P> <p> public class ziputil {</ P> <p>/** <br/> * function: compressed to zip <br/> * @ author Wu Xiaocong added to <br/> * @ Param zipfilep compressed zip file name <br/> * @ Param path compression path <br /> * @ throws Java. lang. exception <br/> */<br/> Public static void zip (string zipfilep, string path) throws exception {<br/> system. out. println ("the compressed file starts ..................... "); <br/> file zipfile = new file (zipfilep); <br/> If (! Zipfile. exists () {<br/> zipfile. createnewfile (); <br/>}< br/> zipoutputstream out = new zipoutputstream (New fileoutputstream (zipfile); <br/> write (Out, path ,""); <br/> out. close (); <br/> system. out. println ("compressed file ended! "); <Br/>}</P> <p>/** <br/> * function: write compression stream <br/> * @ author Wu Xiaocong added to <br/> * @ Param out compression output stream <br/> * @ Param path compression path <br/> * @ Param base: Compressed basic directory <br/> * @ throws Java. lang. exception <br/> */</P> <p> Private Static void write (zipoutputstream out, string path, string Base) <br/> throws exception {<br/> file = new file (PATH); <br/> If (file. isdirectory () {// folder, recursion <br/> base = base. length () = 0? "": Base + file. separator; <br/> file [] tempfiles = file. listfiles (); <br/> for (INT I = 0; I <tempfiles. length; I ++) {<br/> write (Out, tempfiles [I]. getpath (), base + tempfiles [I]. getname (); <br/>}< br/>}else {// file, compressed <br/> byte [] buff = new byte [2048]; <br/> int bytesread =-1; <br/> zipentry entry = new zipentry (base); <br/> out. putnextentry (entry); <br/> inputstream in = new bufferedinputs Tream (New fileinputstream (File); <br/> while (-1! = (Bytesread = in. read (buff, 0, Buff. length) {<br/> out. write (buff, 0, bytesread); <br/>}< br/> in. close (); <br/> out. flush (); <br/>}</P> <p>/** <br/> * function: decompress <br/> * @ author Wu Xiaocong and add it to the file <br/> * @ Param zipfilename zip <br/> * @ Param destpath: Decompress the path <br/> * @ throws java. lang. exception <br/> */<br/> Public static void unzip (string zipfilename, string destpath) throws exception {<B R/> system. out. println ("start to decompress the file ..................... "); <br/> zipfile zip = new zipfile (zipfilename," gb2312 "); <br/> enumeration enu = zip. getentries (); // get all the files in the compressed folder <br/> while (enu. hasmoreelements () {<br/> zipentry entry = (zipentry) enu. nextelement (); <br/> string file = destpath + entry. getname (); <br/> write (ZIP, entry, file); <br/>}< br/> system. out. println ("decompress the file! "); <Br/>}</P> <p>/** <br/> * function: write the decompressed file to the directory <br/> * @ author Wu Xiaocong added to <br/> * @ Param zip compressed file <br/> * @ Param entry compressed file entity -- compress the file <br/> * @ Param filelocation the decompressed file path <br/> * @ throws exception <br/> */<br/> Private Static void write (zipfile zip, zipentry entry, string file) throws exception {<br/> If (entry. isdirectory () {<br/> file F = new file (File); <br/> F. mkdirs (); <br/>}else {<br /> File F = new file (File); <br/> createdir (f); <br/> fileoutputstream Fos = new fileoutputstream (f ); <br/> byte [] buffer = new byte [8196]; <br/> inputstream is = zip. getinputstream (entry); <br/> for (INT Len = is. read (buffer, 0, buffer. length); Len! =-1; Len = is. read (buffer, 0, 8196) {<br/> FOS. write (buffer, 0, Len); <br/>}< br/> FOS. close (); <br/>}</P> <p>/** <br/> * function: create a directory <br/> * @ author Wu Xiaocong added to <br/> * @ Param file or directory <br/> */<br/> Private Static void createdir (file file) {<br/> If (file. isdirectory ()&&! File. exists () {<br/> file. mkdirs (); <br/>}else {<br/> string Path = file. getpath (); <br/> int I = path. lastindexof (file. separator); <br/> Path = path. substring (0, I); <br/> New file (PATH ). mkdirs (); <br/>}</P> <p>/** <br/> * function: test example <br/> * @ author Wu Xiaocong added to <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> try {<br/> zip ("F: // cong.zip", "F: // "); <br/> unzip ("F: // cong.zip "," F: // Cong // "); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>

This source file can be used directly. The key points are as follows:
1. To solve the problem of Chinese garbled characters, you must use the tool toolkit under ant. You only need to put ant. jar in the class path.
2. during decompression, the compressed object is a file. When creating a file, you must first create a folder.

Ant. jar package:

Http://jar.cn13e.com/details.action? Id = 43 & KEYWORDS = ant

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.