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

前端 未结 5 590
臣服心动
臣服心动 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;
    }
    
    0 讨论(0)
  • 2020-12-10 19:30

    can you try below code? For more examples check here http://java2novice.com/java-collections-and-util/zip/unzip/

    FileInputStream fis = null;
        ZipInputStream zipIs = null;
        ZipEntry zEntry = null;
        try {
            fis = new FileInputStream(filePath);
            zipIs = new ZipInputStream(new BufferedInputStream(fis));
            while((zEntry = zipIs.getNextEntry()) != null){
                try{
                    byte[] tmp = new byte[4*1024];
                    FileOutputStream fos = null;
                    String opFilePath = "C:/"+zEntry.getName();
                    System.out.println("Extracting file to "+opFilePath);
                    fos = new FileOutputStream(opFilePath);
                    int size = 0;
                    while((size = zipIs.read(tmp)) != -1){
                        fos.write(tmp, 0 , size);
                    }
                    fos.flush();
                    fos.close();
                } catch(Exception ex){
    
                }
            }
            zipIs.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    0 讨论(0)
  • 2020-12-10 19:37

    Try this Code whic i used to extract all the zip files

    try
        {
    
            final ZipFile zf = new ZipFile("C:/Documents and Settings/satheesh/Desktop/POTL.Zip");
    
            final Enumeration<? extends ZipEntry> entries = zf.entries();
            ZipInputStream zipInput = null;
    
            while (entries.hasMoreElements())
            {
                final ZipEntry zipEntry=entries.nextElement();
                final String fileName = zipEntry.getName();
            // zipInput = new ZipInputStream(new FileInputStream(fileName));
                InputStream inputs=zf.getInputStream(zipEntry);
                //  final RandomAccessFile br = new RandomAccessFile(fileName, "r");
                    BufferedReader br = new BufferedReader(new InputStreamReader(inputs, "UTF-8"));
                    FileWriter fr=new FileWriter(f2);
                BufferedWriter wr=new BufferedWriter(new FileWriter(f2) );
    
                while((line = br.readLine()) != null)
                {
                    wr.write(line);
                    System.out.println(line);
                    wr.newLine();
                    wr.flush();
                }
                br.close();
                zipInput.closeEntry();
            }
    
    
        }
        catch(Exception e)
        {
            System.out.print(e);
        }
        finally
        {
            System.out.println("\n\n\nThe had been extracted successfully");
    
        }
    

    This code really works me in a good manner.

    0 讨论(0)
  • 2020-12-10 19:44

    As i remember this only happen when the file name is not encoded in UTF8.

    If 3rd Component is not forbidden,try Apache Zip API.

    import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile;

    0 讨论(0)
  • 2020-12-10 19:46

    No, zip files are not just for UTF-8 data. Zip files don't try to interpret the data within the files at all, and neither does the Java API.

    There may be issues around non-ASCII names of files, but the file contents themselves shouldn't be a problem at all. In your case, it looks like the name of the file is just test.zip, so you shouldn't be running into any name encoding issues.

    If the file can't be opened, then it sounds like you've got a different problem. Are you sure the file exists where you expect it to be?

    0 讨论(0)
提交回复
热议问题