About using Java to bring Zipoutputstream compressed file name to Chinese garbled problem

Source: Internet
Author: User

Summary
As a result of business requirements, the file system will be a part of the file processing packaging operations, beginning I use the JAVA6 comes with the API processing, when the file name with in, the compressed file after packaging will produce Chinese garbled problem, after hearing that java7 solved this problem, because the industry lag I did not do such an attempt, Interested readers can study it for themselves. Here is a solution for me (Apache under the file Stream tool).

    • Step1: Introducing a jar package
      Http://central.maven.org/maven2/org/apache/ant/ant/1.7.1/ant-1.7.1.jar

    • Step2: Code sample

//Partial Package Introduction SampleImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.util.ArrayList;ImportJava.util.Enumeration;ImportJava.util.List;ImportOrg.apache.log4j.Logger;ImportOrg.apache.tools.zip.ZipEntry;ImportOrg.apache.tools.zip.ZipFile;ImportOrg.apache.tools.zip.ZipOutputStream; Public classZiputil {Private Static FinalLogger Logger = Logger.getlogger (ziputil.class);/** * Function: Compress multiple files into a zip file * @param srcfile: Source file list * @param zipfile: Compressed file */       Public StaticBoolean zipfiles (file[] Srcfile,file zipfile) {Boolean reflag =false;byte[] buf=New byte[10240]; Zipoutputstream out=NULL; FileInputStreaminch=NULL;Try{//zipoutputstream class: Complete compression of files or folders             out=NewZipoutputstream (NewFileOutputStream (ZipFile));//Note Here the encoding settings             out. setencoding ("GBK"); for(intI=0; i<srcfile.length;i++) {inch=NewFileInputStream (Srcfile[i]); out. Putnextentry (NewZipEntry (Srcfile[i].getname ()));intLen while((len=inch. Read (BUF)) >0){ out. Write (BUF,0, Len); } out. Closeentry ();inch. Close ();inch=NULL; } out. Close (); out=NULL; Reflag =true; Logger.info ("Compression complete, file details are:"+zipfile.getabsolutepath ()); }Catch(Exception e)          {E.printstacktrace (); }finally{if(inch!=NULL){Try{inch. Close ();inch=NULL; }Catch(IOException e)                {E.printstacktrace (); }            }if( out!=NULL){Try{ out. Close (); out=NULL; }Catch(IOException e)                {E.printstacktrace (); }            }        }returnReflag; }    }

About using Java to bring Zipoutputstream compressed file name to Chinese garbled problem

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.