Android 中解压 unicode 文件名错误

发布于 2024-11-26 14:55:19 字数 1121 浏览 0 评论 0原文

我有一个 zip 文件,其中包含一个文件:“Indulás előtt.html” (它是匈牙利文本)

但是当我尝试解压缩时,我在 getNextEntry 行中遇到错误:

try {
    ZipInputStream zis = newZipInputStream(getResources().openRawResource(R.raw.ie));
    ZipEntry ze = null;
    while ((ze = zis.getNextEntry()) != null) {
        info.setText(info.getText() + "\nName: " + ze.getName());
    }
} catch (Exception e) {
    info.setText(info.getText() + "\nERROR: " + e.getMessage());
}

错误消息是:“Input at 5 does not match UTF8 specifitcion”

后来我尝试了另一种模式:

ZipFile zipfile = new ZipFile(file);
for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
    ZipEntry entry = (ZipEntry) e.nextElement();
    String name = new String(entry.getName().getBytes("UTF-8"), "UTF-8");
    info.setText(info.getText() + "\nName: " + name);
}

但显示了这个:

图片

解决方案是什么???

文本包括以下字母:

link#1:http://en.wikipedia.org/wiki/%C3%81

link#2:http://en.wikipedia.org/wiki/%C5%90#Hungarian

I have a zip file which is included one file: "Indulás előtt.html"
(it's a hungarian text)

But when I try unzip I got error in the getNextEntry row:

try {
    ZipInputStream zis = newZipInputStream(getResources().openRawResource(R.raw.ie));
    ZipEntry ze = null;
    while ((ze = zis.getNextEntry()) != null) {
        info.setText(info.getText() + "\nName: " + ze.getName());
    }
} catch (Exception e) {
    info.setText(info.getText() + "\nERROR: " + e.getMessage());
}

and the error message is: "Input at 5 does not match UTF8 specifitcion"

Later I tried in another mode:

ZipFile zipfile = new ZipFile(file);
for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
    ZipEntry entry = (ZipEntry) e.nextElement();
    String name = new String(entry.getName().getBytes("UTF-8"), "UTF-8");
    info.setText(info.getText() + "\nName: " + name);
}

but displayed this:

Image

What is the solution???

The text include this letters:

link#1:http://en.wikipedia.org/wiki/%C3%81

link#2:http://en.wikipedia.org/wiki/%C5%90#Hungarian

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

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

发布评论

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

评论(1

城歌 2024-12-03 14:55:19

zip 文件的文件名字符集可能不明确。 Java 7 的 zip 实现应该能够检测 UTF-8 标志 (http://docs.oracle.com/javase/7/docs/api/java/util/zip/package-summary.html#lang_encoding)但这依赖于打包申请到已正确编码文件名并设置必需的 UTF-8 标志。

我怀疑您的 zip 文件打包不正确或未使用 UTF-8 文件名。尝试传递默认的 Zip 字符集:

Cp437

ZipInputStream zis = new ZipInputStream(getResources().openRawResource(R.raw.ie), Charset.forName("Cp437"));

The filename character-set of a zip file can be ambiguous. Java 7's zip implementation should be able to detect the UTF-8 flag (http://docs.oracle.com/javase/7/docs/api/java/util/zip/package-summary.html#lang_encoding) but this relies on the packaging application to have correctly encoded the filename and set the requisite UTF-8 flag.

I suspect that your zip file has been packaged incorrectly or not using UTF-8 filename. Trying passing the default Zip char-set: Cp437

E.g.

ZipInputStream zis = new ZipInputStream(getResources().openRawResource(R.raw.ie), Charset.forName("Cp437"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文