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

前端 未结 5 591
臣服心动
臣服心动 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

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

提交回复
热议问题