sun.jnu.encoding 影响文件名的创建,而 file.encoding 则影响到文件内容。
所以说,在我们使用 Java 处理中文文件的时候,如果发现文件的中文内容没有乱码,而文件的中文名发生乱码,我们就应当多考虑一下 sun.jnu.encoding 和 file.encoding 的区别了。
支持中文文件目录的 org.apache.tools.zip.* 打包解压缩中文文件夹都没有问题的。但有的时候打包后的中文文件名会出现乱码,这是为什么呢?查看 org.apache.tools.zip.ZipOutputStream 的 API 会发现关于其 getEncoding() 方法的说明:“The encoding to use for filenames and the file comment.”,将以下语句执行一下:

org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(new java.io.FileOutputStream(“D:/temp/testfile.zip”));
System.out.println(“out.getEncoding()=” + out.getEncoding());

执行结果如下:

out.getEncoding()=null

问题就在这里了。在 org.apache.tools.zip.ZipOutputStream 创建好以后,手工设置一下:

out.setEncoding(System.getProperty(“sun.jnu.encoding”));

发现打包后中文名可以正常显示了。
参考一:http://stackoverflow.com/questions/1066845/what-exactly-is-sun-jnu-encoding【stackoverflow 关于 sun.jnu.encoding 的解答】,
参考二:http://www.jajakarta.org/ant/ant-1.6.1/docs/ja/manual/api/org/apache/tools/zip/ZipOutputStream.html【ZipOutputStream 的 API】,
参考三:http://massapi.com/source/apache-ant-1.8.2/src/main/org/apache/tools/zip/ZipOutputStream.java.html【ZipOutputStream 的源代码】。
来源:http://blog.csdn.net/defonds/article/details/7044750

: https://blog.darkmi.com/2017/06/23/3944.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.