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

Source: Internet
Author: User

When extracting jar files or zip files 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 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 folder, with WinRAR to make a demo.zip file, execute the above code will find thrown exception. This is due to the Chinese Windows environment, the operating system default character set is GBK, but ZipFile can only recognize the UTF-8 format. So when the zip file contains Chinese (but not the UTF-8 code), Java's zipfile will be an error.

The problem is very easy to solve in JDK1.7 and beyond. The JDK provides a constructor that agrees with the character set we specify for the zip file.

Public ZipFile (String name, Charset Charset) throws IOException  


It is assumed that the org.apache.tools.zip.ZipFile provided in Ant.jar can be used in a manner similar to ZipFile in JDK1.7 before JDK1.6 or before .

can see JDK1.7 zipfile agree we specify the character set, but jarfile this class or fixed use 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.