5

How to decompress a zip file with unicode filename? This is my code:

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

but I receive the error: 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) 

My class is ZipManager.

How to resolve this problem?

Danielson
  • 2,605
  • 2
  • 28
  • 51
shuttle1978
  • 123
  • 2
  • 12
  • Provide please full stacktrace of exception – Andremoniy Jun 05 '13 at 08:47
  • 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) My class is ZipManager – shuttle1978 Jun 05 '13 at 08:49
  • Are you sure that the filenames are UTF-8 encoded? It looks like decoding the UTF-8 filename failes. – Uwe Plonus Jun 05 '13 at 09:05
  • How file names are encoded depends on the operating system. Are you on Windows, OS X, Linux, or something else? – Joni Jun 05 '13 at 11:41
  • 1
    Hi Shuttle1978, I just encounter the same problem here. Did you solve it at last? – Bowie Jan 05 '15 at 07:34
  • I have the same problem! please provide your solution. – Behrouz.M Jan 05 '15 at 16:30

1 Answers1

0

According to the exception, it seems that your file is actually not in UTF-8 encoding.

Furthermore, the problem is not with file's name. This line:

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

is meaningless, because ze.getName() is already correct java string.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241