Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unzip file that that is not in UTF8 format in java

Tags:

java

I have a file e.g. test.zip. If I use a ZIP-tool like winrar, it's easy to extract (unzip test.zip to test.csv). But test.csv is not in UTF8 format. My problem here is, when I use java to unzip it, it can't read this file.

ZipFile zf = new ZipFile("C:/test.zip");

The thrown exception says that there occurs an error by opening that file.

On java http://java.sun.com/developer/technicalArticles/Programming/compression/ is nothing written about data formatting. Maybe the whole API is designed only for UTF8-format data. So, if I have to unzip data except UTF8 format, how to unzip it? Especially the japanese and chinese characters that holds more space size (except UTF8). I also found an API at http://truezip.java.net/6/tutorial.html where this problem is mentioned. But, I didn't get a way on how to solve it. Is there any simple way to solve this problem? Especially from the API that is passed from JAVA specification request.

like image 216
abishkar bhattarai Avatar asked Feb 19 '23 16:02

abishkar bhattarai


1 Answers

JDK6 has a bug in java.util.zip implementation it cannot handle non-USASCII characters. I use Apache Commons commons-compress-1.0.jar library to fix it. JDK7 has fixed java.util.zip implementation. http://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipInputStream.html

import java.io.*;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.*;

public static int unzip(File inputZip, File outputFolder) throws IOException {
    int count=0;
    FileInputStream fis = null;
    ZipArchiveInputStream zis = null;
    FileOutputStream fos = null;
    try {
        byte[] buffer = new byte[8192];
        fis = new FileInputStream(inputZip);
        zis = new ZipArchiveInputStream(fis, "Cp1252", true); // this supports non-USACII names
        ArchiveEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            File file = new File(outputFolder, entry.getName());
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                count++;
                file.getParentFile().mkdirs();
                fos = new FileOutputStream(file);
                int read;
                while ((read = zis.read(buffer,0,buffer.length)) != -1)
                    fos.write(buffer,0,read);
                fos.close();
                fos=null;
            }
        }
    } finally {
        try { zis.close(); } catch (Exception e) { }
        try { fis.close(); } catch (Exception e) { }
        try { if (fos!=null) fos.close(); } catch (Exception e) { }
    }
    return count;
}
like image 124
Whome Avatar answered Mar 03 '23 04:03

Whome