`

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics