Java:从Zip文件中读取带有特殊字符的文件

huangapple go评论78阅读模式
英文:

Java : Read a file with special characters from a Zip file

问题

我有一个带有以下内容的压缩文件:

Java:从Zip文件中读取带有特殊字符的文件

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:

Java:从Zip文件中读取带有特殊字符的文件

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(&quot;Test.zip&quot;, Charset.forName(&quot;UTF-8&quot;));

            Enumeration&lt;? extends ZipEntry&gt; 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(&quot;Test.zip&quot;)) { //UTF-8 by default
        Enumeration&lt;ZipArchiveEntry&gt; 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);
    }
}

huangapple
  • 本文由 发表于 2020年10月20日 23:40:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64448616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定