安卓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 解压,自测成功。

猜你喜欢

转载自blog.csdn.net/lyjSmile/article/details/81913602
今日推荐