美文网首页
【Android】Zip文件解压方法

【Android】Zip文件解压方法

作者: 心安1989 | 来源:发表于2019-05-05 10:12 被阅读0次

    android中zip文件解压

    public class ZipUtil {
        /**
         * 解压到指定路径
         *
         * @param inputStream
         * @param outPathString
         * @throws Exception
         */
        public static void UnZipFolder(InputStream inputStream, String outPathString) throws Exception {
            ZipInputStream inZip = new ZipInputStream(inputStream);
            ZipEntry zipEntry;
            String szName = "";
            File root = new File(outPathString);
            if (!root.exists()) {
                root.mkdir();
            }
            while ((zipEntry = inZip.getNextEntry()) != null) {
                szName = zipEntry.getName();
                if (zipEntry.isDirectory()) {
                    //获取部件的文件夹名
                    szName = szName.substring(0, szName.length() - 1);
                    File folder = new File(outPathString + File.separator + szName);
                    folder.mkdirs();
                } else {
                    Log.i("tag", outPathString + File.separator + szName);
                    File file = new File(outPathString + File.separator + szName);
                    if (!file.exists()) {
                        Log.i("tag", "Create the file:" + outPathString + File.separator + szName);
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                    }
                    // 获取文件的输出流
                    FileOutputStream out = new FileOutputStream(file);
                    int len;
                    byte[] buffer = new byte[2048];
                    // 读取(字节)字节到缓冲区
                    while ((len = inZip.read(buffer)) != -1) {
                        // 从缓冲区(0)位置写入(字节)字节
                        out.write(buffer, 0, len);
                        out.flush();
                    }
                    out.close();
                }
            }
            inZip.close();
        }
    }
    

    相关文章

      网友评论

          本文标题:【Android】Zip文件解压方法

          本文链接:https://www.haomeiwen.com/subject/xxhcoqtx.html