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

Viewed 321

I have a zip file with the following content:

enter image description here

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 Answers

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);
    }
}
Related