Java Operation Zip compressed file encountered encoding problem

Source: Internet
Author: User

1, encountered the problem: in the For loop operation Zip file (the file name contains Chinese), the error is as follows:

Exception in thread "main" Java.lang.IllegalArgumentException:MALFORMEDat java.util.zip.ZipCoder.toString ( zipcoder.java:58) at Java.util.zip.ZipInputStream.readLOC (zipinputstream.java:297) at Java.util.zip.ZipInputStream.getNextEntry (zipinputstream.java:121) at Com.forestfood.FileUntils.done ( fileuntils.java:224) at Com.forestfood.FileUntils.getActionZipFiles (fileuntils.java:196) at Com.forestfood.FileUntils.main (fileuntils.java:24)

2, find the reason: tracking error code line to zipcoder.java:58

Final class zipcoder {    string tostring (Byte[] ba, int  length)  {        charsetdecoder cd = decoder (). Reset ();        int len =  (int) (length *  Cd.maxcharsperbyte ());         char[] ca = new char [len];        if  (len == 0)              return new string (CA);         // UTF-8 only for now. Other ArrayDeocder only  handles        // codingerroraction.replace mode.  zipcoder uses        // report mode.         if  (Isutf8 && cd instanceof arraydecoder)  {             int clen =  ((Arraydecoder) CD). Decode (ba, 0 ,  length, ca);            if  (Clen  == -1)     // malformed                 throw new illegalargumentexception ("MALFORMED");             return new string (ca, 0 ,  clen);         }

It can be found that if you do not specify the encoding, the default is UTF-8, when decoding the error will occur.

3. Workaround: When creating the ZIP, specify the encoding

FileInputStream FileInputStream = new FileInputStream (Zippath); Charset GBK = Charset.forname ("GBK"); Zipinputstream Zin = new Zipinputstream (FILEINPUTSTREAM,GBK); ZipEntry entry = Zin.getnextentry ();


Java Operation Zip compressed file encountered encoding 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.