android asset中 zip包解压sdcard

  //定义assetmanager对象
                         AssetManager assetManager = getAssets();
                         // 需要解压的对象
                            InputStream dataSource = assetManager.open("ShiningTrip.zip");
                            //    調用解压的方法
                            ZipUtil.unzip(dataSource, android.os.Environment
                                    .getExternalStorageDirectory()  + "");




    public static void unzip(InputStream zipFileName, String outputDirectory) {
        try {
            ZipInputStream in = new ZipInputStream(zipFileName);
            // 获取ZipInputStream中的ZipEntry条目,一个zip文件中可能包含多个ZipEntry,
            // 当getNextEntry方法的返回值为null,则代表ZipInputStream中没有下一个ZipEntry,
            // 输入流读取完成;
            ZipEntry entry = in.getNextEntry();
            while (entry != null) {

                // 创建以zip包文件名为目录名的根目录
                File file = new File(outputDirectory);
                file.mkdir();
                if (entry.isDirectory()) {
                    String name = entry.getName();
                    name = name.substring(0, name.length() - 1);
             
                    file = new File(outputDirectory + File.separator + name);
                    file.mkdir();
              
                } else {
                    file = new File(outputDirectory + File.separator + entry.getName());
                    file.createNewFile();
                    FileOutputStream out = new FileOutputStream(file);
                    int b;
                    while ((b = in.read()) != -1) {
                        out.write(b);
                    }
                    out.close();
                }
                // 读取下一个ZipEntry
                entry = in.getNextEntry();
            }
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值