gpt4 book ai didi

java用unicode文件名解压zipfile

转载 作者:搜寻专家 更新时间:2023-11-01 03:53:27 25 4
gpt4 key购买 nike

如何解压文件名为 unicode 的 zip 文件?这是我的代码:

try {
ZipInputStream zis = new ZipInputStream(
new FileInputStream(zipFile));
ZipEntry ze = zis.getNextEntry();

System.setProperty("file.encoding", "UTF-8");
while (ze != null) {
String fileName = new String(ze.getName().getBytes("UTF-8"));
System.out.println(fileName);
File newFile = new File(outputFolder + File.separator + fileName );

BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(newFile));
OutputStreamWriter osw = new OutputStreamWriter(outStream, Charset.forName("UTF-8"));
int ch;
StringBuffer buffer1 = new StringBuffer();
while ((ch = zis.read()) > -1) {
buffer1.append((char) ch);
}
osw.write(buffer1.toString());
osw.close();
outStream.close();

ze = zis.getNextEntry();
}

zis.closeEntry();
zis.close();
} catch (IOException ex) {
ex.printStackTrace();
}

但我收到错误:UTFDataFormatException:

06-05 08:46:33.394: W/System.err(777): java.io.UTFDataFormatException: bad second or third byte at 6 
06-05 08:46:33.394: W/System.err(777): at java.nio.charset.ModifiedUtf8.decode(ModifiedUtf8.java:56)
06-05 08:46:33.426: W/System.err(777): at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:270)
06-05 08:46:33.426: W/System.err(777): at com.learnlang.utility.ZipManager.unZipIt(ZipManager.java:62)
06-05 08:46:33.434: W/System.err(777): at com.learnlang.HomeActivity$progressThread.run(HomeActivity.java:317)

我的类是 ZipManager

如何解决这个问题?

最佳答案

根据异常,您的文件似乎实际上不是 UTF-8 编码。

此外,问题不在于文件名。这一行:

 String fileName = new String(ze.getName().getBytes("UTF-8"));

没有意义,因为ze.getName()已经是正确的java字符串了。

关于java用unicode文件名解压zipfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16935225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com