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

后端 未结 3 1728
一生所求
一生所求 2021-01-23 05:50

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 unrea

相关标签:
3条回答
  • 2021-01-23 06:38

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

    0 讨论(0)
  • 2021-01-23 06:39

    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();
       }
    }
    
    0 讨论(0)
  • 2021-01-23 06:51

    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).

    0 讨论(0)
提交回复
热议问题