Android 中解压 unicode 文件名错误
我有一个 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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
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.