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