在zip存档中使用Unicode字符表示文件名 [英] Using Unicode characters for file names inside a zip archive
问题描述
我荏苒文件名包含一些特殊字符,例如PéréquationLES HOPITAUX NEUFS.xls ,以不同的文件夹,说的临时
I am zipping a file name contains some special characters like Péréquation LES HOPITAUX NEUFS.xls to a different folder, say temp.
我可以压缩文件,但问题是文件名自动更改为
P +¬r+¬quationLES HOPITAUX NEUFS.xls 。
I am able to zip the file but the problem is the name of file is changing automatically to P+¬r+¬quation LES HOPITAUX NEUFS.xls.
如何在zip存档中支持文件名的unicode字符?
How can I support unicode characters for file names inside a zip archive?
推荐答案
这取决于您用于创建存档的代码。 旧 Java压缩类不是那么灵活。
It depends a little bit on what code you're using to create the archive. The old Java compression classes are not so flexible as you need.
您可以使用 Apache Commons Compress 。 迈克尔西门子写了这段很好的代码:
You may use Apache Commons Compress. Michael Simons wrote this nice piece of code:
ZipArchiveOutputStream ostream = ...; // Your initialization code here
ostream.setEncoding("Cp437"); // This should handle your "special" characters
ostream.setFallbackToUTF8(true); // For "unknown" characters!
ostream.setUseLanguageEncodingFlag(true);
ostream.setCreateUnicodeExtraFields(
ZipArchiveOutputStream.UnicodeExtraFieldPolicy.NOT_ENCODEABLE);
如果你使用的是Java 7 那么你终于得到了 Charset
参数(可以是UTF-8) ZipOutputStream构造
If you're using Java 7 then you finally have a Charset
parameter (that can be UTF-8) on the ZipOutputStream constructor
在大问题,反正就是很多实现不理解Unicode编码因为原始 ZIP文件格式是ASCII,并且没有Unicode的官方标准。请参阅进一步的细节此帖子
The big problem, anyway, is that many implementations don't understand Unicode encoding because original ZIP file format is ASCII and there is not an official standard for Unicode. See this post for further details.
这篇关于在zip存档中使用Unicode字符表示文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!