英文:
Java : Read a file with special characters from a Zip file
问题
我有一个带有以下内容的压缩文件:
在 Temperature_°C.log 中的内容为:unit°C
我使用以下代码来打印压缩文件中的所有文件名:
public static void main(String[] args) {
try {
ZipFile zipFile = new ZipFile("Test.zip", Charset.forName("UTF-8"));
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
try {
ZipEntry zipEntry = entries.nextElement();
System.out.println(zipEntry.getName());
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
zipFile.close();
} catch (IOException ex) {
Logger.getLogger(ZipTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
在代码的这一行:ZipEntry zipEntry = entries.nextElement();
对于 Temperature_°C.log,它抛出 java.lang.IllegalArgumentException: MALFORMED
我尝试过 UTF-8
,但不起作用。当我尝试使用 ISO-8859-1
时,显示了乱码字符。
我应该如何解决这个问题?
英文:
I have a zip file with the following content:
The content inside Temperature_°C.log : unit°C
and i use the following code to print all the file names inside a zip:
public static void main(String[] args) {
try {
ZipFile zipFile = new ZipFile("Test.zip", Charset.forName("UTF-8"));
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
try {
ZipEntry zipEntry = entries.nextElement();
System.out.println(zipEntry.getName());
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
zipFile.close();
} catch (IOException ex) {
Logger.getLogger(ZipTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
and at line : ZipEntry zipEntry = entries.nextElement();
for the Temperature_°C.log it throws java.lang.IllegalArgumentException: MALFORMED
I tried UTF-8
and it is not working. When i tried with ISO-8859-1
it displays junk character.
How should i solve this ?
答案1
得分: 1
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
public static void main(String[] args) {
try(ZipFile zipFile = new ZipFile("Test.zip")) { //UTF-8 by default
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
try {
ZipArchiveEntry zipEntry = entries.nextElement();
System.out.println(zipEntry.getName());
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
} catch (IOException ex) {
Logger.getLogger(ZipTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
英文:
Got the same problem, but with cyrillic characters. Had to use commons-compress library instead of standard.
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
public static void main(String[] args) {
try(ZipFile zipFile = new ZipFile("Test.zip")) { //UTF-8 by default
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
try {
ZipArchiveEntry zipEntry = entries.nextElement();
System.out.println(zipEntry.getName());
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
} catch (IOException ex) {
Logger.getLogger(ZipTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论