gpt4 book ai didi

java - 解压带有重音符号的 zip 文件

转载 作者:行者123 更新时间:2023-11-29 06:02:50 24 4
gpt4 key购买 nike

我正在使用 Java 机制来提取 zip 文件。如果其中没有标题带有重音的文件,该机制就可以正常工作。因为我来自葡萄牙,我的语言中通常使用 ã、ç、õ、é 等字符。如果文件名中有任何这些字符,则会发生 IO 异常。

while (zipFileEntries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
File destFile = new File(unzipDestinationDirectory, currentEntry);
File destinationParent = destFile.getParentFile();

// create the parent directory structure if needed
destinationParent.mkdirs();

// extract file if not a directory
if (!entry.isDirectory()) {
BufferedInputStream is =
new BufferedInputStream(zip_file.getInputStream(entry));
int currentByte;
byte data[] = new byte[BUFFER];

// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}

dest.flush();
dest.close();
is.close();
}

它在 while((currentByte = is.read(data, 0, BUFFER)) != -1)

时崩溃
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:134)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
at parsers.ZipParser.decompressZipFile(ZipParser.java:83)
at poc.MainPOC.main(MainPOC.java:61)

您是否知道解决此问题的任何解决方法?我可以在不解压缩的情况下更改 zip 文件的文件名吗?

最佳答案

从 Java 7 开始,ZipInputStream 有了新的构造函数来指定用于文件名的字符集。请参阅文档 here .

所以你会创建你的 ZipInputStream 像这样:

ZipInputStream zis = new ZipInputStream(new FileInputStream("your zip file"), Charset.forName("Encoding here"));

参见 Charset了解有关如何使用它的更多信息。

它不会改变您阅读文件的方式,因此您需要另一种解决方法来阅读内容。但有关更多信息,请参阅此答案 Java zip character encoding ,您可能可以重复使用一些代码。

关于java - 解压带有重音符号的 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9514243/

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