安卓ZipInputStream 解壓文件

/**
* 解壓文件
* 將目標文件解壓到指定目錄
* @param srcFile 源文件
* @param desFile 目標文件
* @throws IOException
*/
public static void unZip(File srcFile, File desFile) throws IOException {

    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(srcFile),
            BUFFER_SIZE));
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            new File(desFile, ze.getName()).mkdirs();
        } else {
            File file = new File(desFile, ze.getName());
            //                File parentdir = file.getParentFile();
            //                if (parentdir != null && (!parentdir.exists())) {
            //                    parentdir.mkdirs();
            //                }
            if (file.exists()) {
                file.delete();
            }
            int pos;
            byte[] buf = new byte[BUFFER_SIZE];
            OutputStream bos = new FileOutputStream(file);
            while ((pos = zis.read(buf, 0, BUFFER_SIZE)) > 0) {
                bos.write(buf, 0, pos);
            }
            bos.flush();
            bos.close();
        }
    }
    zis.close();
}

之前用GZipInputStream解壓Zip文件,報錯文件不合法Gzip。可能需要在網絡傳輸流中設置支持Gzip。
然後換了種方式解壓,試試用ZipInputStream 解壓,自測成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章