.zip file created by Java doesn't support Chinese(utf-8)

跟風遠走 提交于 2019-12-20 03:21:37

问题


I want to create a .zip file using Java(jdk, ant.jar or commons-compress).

But if the ZipEntry's name contains non-English(eg. Chinese, Japanese), it will display unreadable code in WinRAR or Windows Compress(commons-compress display correctly in WinRAR).

Who can help me!!!


回答1:


You have hit one of the Top 25 java bug.

Good news is this is already resolved. Bad news it it is fixed only in JDK7. See this entry for details.

Alternativlly, you can use Arcmexer (readonly).




回答2:


try this by using apache commons compress,

import java.io.*;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
public class ZipFiles {  
   public static void main(String[] args) throws Exception{
       ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(new FileOutputStream("测试.zip"));
       zipOut.setEncoding("Cp437"); // This should handle your "special" characters
       zipOut.setFallbackToUTF8(true); // For "unknown" characters!
       zipOut.setUseLanguageEncodingFlag(true);                               
       zipOut.setCreateUnicodeExtraFields(
       ZipArchiveOutputStream.UnicodeExtraFieldPolicy.NOT_ENCODEABLE);
       zipOut.putArchiveEntry(new ZipArchiveEntry("测试.xml"));
       zipOut.putArchiveEntry(new ZipArchiveEntry("test.xml"));
       zipOut.closeArchiveEntry();
       zipOut.flush();
       zipOut.close();
   }
}



回答3:


Take a look to 7-Zip-JBinding it's a Java wrapper for 7zip.



来源:https://stackoverflow.com/questions/4212577/zip-file-created-by-java-doesnt-support-chineseutf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!