java 如何将带有 utf-8 名称的 zip 条目添加到 zip

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15519493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:54:16  来源:igfitidea点击:

How to add zip entry with utf-8 name to zip

javazip

提问by hudi

I have a method which adds inputStream to zip as an entry:

我有一个方法将 inputStream 添加到 zip 作为条目:

private void addToZip(InputStream is, String filename) throws Exception {
    try {
        ZipEntry zipEntry = new ZipEntry(filename);
        zos.putNextEntry(zipEntry);
        byte[] bytes = new byte[1024];
        int length;
        while ((length = is.read(bytes)) >= 0) {
            zos.write(bytes, 0, length);
        }
        zos.closeEntry();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

The problem occurs when the filename contains an UTF-8 char like áé... In zip file it will be saved as ?????and when I unzip it in ubuntu 12.10 it looks like: N├бstrojeinstead of Nástroje.

当文件名包含UTF-8字符像AE将出现问题...在zip文件将被保存为?????当我在Ubuntu 12.10将它解压缩它看起来像:N├бstroje不是Nástroje

For this example I used jdk6 but now I've also tried jdk7:

在这个例子中,我使用了 jdk6,但现在我也尝试了 jdk7:

zos = new ZipOutputStream(fos, Charset.forName("UTF-8"));

But with no success.

但没有成功。

I also tried Apache Commons Zip and set encoding but also with no success.

我也尝试过 Apache Commons Zip 并设置编码,但也没有成功。

So how I can add this file with unicode symbols in filename to zip ?

那么如何将文件名中带有 unicode 符号的文件添加到 zip 呢?

采纳答案by hudi

seems this line solved my problem:

似乎这条线解决了我的问题:

        zos.setCreateUnicodeExtraFields(UnicodeExtraFieldPolicy.ALWAYS);

can someone explain me what is this doing and why it works ?

有人可以解释我这是在做什么以及它为什么起作用吗?

回答by Nickolay Olshevsky

Zip archive by default uses DOS(OEM) codepage to store filenames. Linux/unix implementations uses system codepage when unpacking. Mac OS uses utf-8 by default. So in your case filename is stored correctly, but Linux archiver doesn't understand it.

Zip 存档默认使用 DOS(OEM) 代码页来存储文件名。Linux/unix 实现在解包时使用系统代码页。Mac OS 默认使用 utf-8。因此,在您的情况下,文件名存储正确,但 Linux 归档程序无法理解。