Java创建的.zip文件不支持中文(utf-8)

发布于 2024-10-02 17:13:21 字数 182 浏览 0 评论 0原文

我想使用 Java(jdk、ant.jar 或 commons-compress)创建 .zip 文件。

但如果 ZipEntry 的名称包含非英文(例如中文、日文),它将在 WinRAR 或 Windows 压缩中显示不可读的代码(commons-compress 在 WinRAR 中正确显示)。

谁能帮帮我!!!

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!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝颜夕 2024-10-09 17:13:21

您遇到了 前 25 个 Java bug 之一。

好消息是这个问题已经解决了。坏消息是它仅在 JDK7 中得到修复。有关详细信息,请参阅此条目

或者,您可以使用 Arcmexer (只读)。

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

旧街凉风 2024-10-09 17:13:21

使用 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();
   }
}

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();
   }
}
魔法少女 2024-10-09 17:13:21

查看7-Zip-JBinding,它是 7zip 的 Java 包装器。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文