简体   繁体   中英

File names with Japanese characters turn to garbage when written to a zip file using java.util.zip.*

I have a directory with a name that contains Japanese characters, and I need to use the zip utils in java.util.zip to write it to a zip file. Writing the zip file succeeds, but when I open the resulting zip file with either Windows' built-in compressed file utility or 7-Zip, the directory with Japanese characters in the name appears as a bunch of garbage characters. I do have the Japanese/East Asian language pack installed on my system -- I can create directories with Japanese names, so that isn't the issue.

Interestingly, if I write a separate script to read the resulting zip file using java.util.zip, the directory name is correct, and I can extract the contents of the zip into appropriately named directories, with Japanese characters. But I can't do this using the commercial zip tools that I've tried, which is undoubtedly what our customers will want to do.

Any ideas about what is causing this problem, and how I can work around it?

I know about this bug , but I still need a workaround for this case.

TrueZIP claims to do this better:

The J2SE API always uses UTF-8 (eight bit Unicode character set) for entry names and comments instead of CP437 (aka IBM437, the genuine IBM-PC character set), which is used by the de-facto standard PKZIP from PKWARE. As a result, you cannot read or write ZIP files with international entry file names such as eg "täscht.txt" in a ZIP file created by a (southern) German.

[description of other problems omitted]

The TrueZIP Library has been developed to overcome these limitations/disadvantages.

Miracles indeed happen, and Sun/Oracle did really fix the long-living bug/rfe:

Now it's possible to [set up filename encodings upon creating][1] the zip file/stream ( requires Java 7 ).

[1]: http://download.java.net/jdk7/docs/api/java/util/zip/ZipOutputStream.html#ZipOutputStream(java.io.OutputStream , java.nio.charset.Charset)

If java.util.zip still behaves as this post describes , I'm not sure if it is possible (with the built-in classes). I have seen Chilkat's Java Zip library mentioned before as a way to get this to work, but have never used it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM