Solve the problem of Chinese path and garbled characters in Java ZipFile decompression

Source: Internet
Author: User

When unpacking a jar file or zip file in Java, you can use the built-in Api:jarfile and ZipFile of the JDK to extract the 2 format files under Windows, often reporting the following error:

Exception in thread "main" Java.lang.IllegalArgumentException:MALFORMEDat java.util.zip.ZipCoder.toString ( zipcoder.java:58) at Java.util.zip.ZipFile.getZipEntry (zipfile.java:531) at java.util.zip.zipfile.access$900 ( zipfile.java:56) at Java.util.zip.zipfile$1.nextelement (zipfile.java:513) at Java.util.zip.zipfile$1.nextelement ( zipfile.java:483)


There are 2 possible types of problems:

1, get the zip or jar itself is damaged, this time can use WinRAR and other software to decompress.

If there is no error, then you can prove that: get the compressed file is not a problem.

2, Character set problem, zip or jar contains the Chinese file name or file path.

Windows itself uses WinRAR to compress files file = new ("C:/users/aty/desktop/demo.zip"); ZipFile zip = new ZipFile (file); enumeration<? Extends zipentry> Entrys = Zip.entries (); while (Entrys.hasmoreelements ()) {ZipEntry entry = entrys.nextelement (); System.out.println (Entry.getname ());} Zip.close ();

Under Windows, the name contains the Chinese file or directory, with WinRAR to make a demo.zip file, run the above code will find thrown exception. This is because the default character set for the operating system is GBK in the Chinese Windows environment, but ZipFile only recognizes UTF-8 format. So when the zip file contains Chinese (but not UTF-8 encoding), the Java ZipFile will get an error.

This problem is easy to solve in JDK1.7 and beyond. The JDK provides a constructor that allows us to specify the character set of the zip file.

Public ZipFile (String name, Charset Charset) throws IOException  


If you are JDK1.6 or before, you can use the org.apache.tools.zip.ZipFile provided in Ant.jar, which is similar to ZipFile in JDK1.7.

you can see that zipfile in JDK1.7 allows us to specify a character set, but jarfile this class is still fixed using UTF-8 format.

Solve the problem of Chinese path and garbled characters in Java ZipFile decompression

Related Article

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.