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

前端 未结 5 598
臣服心动
臣服心动 2020-12-10 19:24

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, whe

5条回答
  •  庸人自扰
    2020-12-10 19:30

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

提交回复
热议问题