欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于Obb的使用

程序员文章站 2024-03-16 17:58:04
...

obb文件在Android -- obb -- 对应的项目包名下。此文件用于Assets压缩使用。由于谷歌市场发行的包必须小于100M。所以我们可以将需要的资源再此打包。

obb格式为 main.版本号.包名。

将Assets文件进行打包,然后通过zip压缩,改后缀名即可。

    //获取Obb包
    public  String getObbFilePath(Context context) {
        try {
            return Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/Android/obb/"
                    + context.getPackageName()
                    + File.separator
                    + "main."
                    + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode
                    + "."
                    + context.getPackageName()
                    + ".obb";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
    //解压Obb包
    public void unZipObb(Context context) {
     
        String obbFilePath = getObbFilePath(context);
        if (obbFilePath == null) {
            return;
        } else {
            File obbFile = new File(obbFilePath);

            //解压路径为    Android/data/包名/files下(后续路径为main.版本号.包名)
            File outputFolder = context.getExternalFilesDir(null);
            if (!outputFolder.exists()) {
                outputFolder.mkdirs();
            }

            File obbfile = context.getExternalFilesDir("main." + Constant.getVersionCode(context) + "." + Constant.PageName);
            if(obbfile.exists()){
                /**
                 * 判断是否解压完毕已经该文件是否是解压的
                 * 判断条件为该文件夹下是否具有该文件
                 */
                if(!FileUtil.isNullFile(obbFile)){
                    try {
                        unZip(obbFile, outputFolder.getAbsolutePath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }else{
                try {
                    unZip(obbFile, outputFolder.getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    //解压资源
    private  void unZip(File zipFile, String outPathString) throws IOException {
        CreateDirectoryIfNeeded(outPathString);
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry zipEntry;
        String szName;
        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 {
                File file = new File(outPathString + File.separator + szName);
                CreateDirectoryIfNeeded(file.getParent());
                file.createNewFile();
                FileOutputStream out = new FileOutputStream(file);
                int len;
                byte[] buffer = new byte[1024];
                while ((len = inZip.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                    out.flush();
                }
                out.close();
            }
        }
        inZip.close();
    }
   //obb内解压资源拷贝
    private void copyMusicFile(String fromFile, String toFile){
        try {
            FileInputStream inputStream = new FileInputStream(new File(fromFile));
            byte[] data = new byte[1024];
            FileOutputStream outputStream =new FileOutputStream(new File(toFile));
            while (inputStream.read(data) != -1) {
                outputStream.write(data);
            }
            inputStream.close();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //判断该文件夹是否是空文件夹
    public static boolean isNullFile(File file) {
        if(file.exists()){
            String[] list = file.list();
            if (list != null) {
                if(list.length>0){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
        return false;
    }

原文链接:https://www.jianshu.com/p/ff1f13ba7577?tdsourcetag=s_pcqq_aiomsg