Android obb解压

本文介绍了一个用于处理Android平台上的OBB文件的方法,包括获取OBB文件路径、创建必要的目录、解压OBB文件到指定路径,以及一个专门针对OBB文件的解压流程,确保了游戏资源的正确安装与更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    // obb
    public static String getObbFilePath(Context context) {
        try {
            Log.d("bbt", "getObbFilePath : packetName=" + context.getPackageName());
            Log.d("bbt", "getObbFilePath : absolutePath=" + Environment.getExternalStorageDirectory().getAbsolutePath());
            Log.d("bbt", "getObbFilePath : versionCode=" + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
            return Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/Android/obb/"
                    + context.getPackageName()
                    + File.separator
                    + "main."
                    + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode
                    + "."
                    + context.getPackageName()
                    + ".obb";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void createDirectoryIfNeeded(String folderPath) {
        File folder = new File(folderPath);
        if (!folder.exists() || !folder.isDirectory()) {
            folder.mkdirs();
        }
    }

    public static void unZip(File zipFile, String outPathString) {
        Log.d("bbt", "unZip " + zipFile.getName() + " to " + outPathString);
        try {
            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();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void unZipObb(String srcFilePath, String dstFolderPath) {
        Log.d("bbt", "unZipObb : srcFilePath=" + srcFilePath + ", dstFolderPath="+dstFolderPath);
        Context context = m_pActivity;
        if (context == null) {
            Log.d("bbt", "unZipObb error : context == null");
            return;
        }

        try {
            String obbFilePath = srcFilePath;
            if (obbFilePath.equals("")) {
                obbFilePath = getObbFilePath(context);
            }
            if (obbFilePath == null) {
                Log.d("bbt", "unZipObb error : obbFilePath == null");
                return;
            } else {
                File obbFile = new File(obbFilePath);
                if (!obbFile.exists()) {
                    //下载obb文件
                    Log.d("bbt", "unZipObb error : !obbFile.exists()");
                } else {
                    File outputFolder = new File(dstFolderPath);
                    if (!outputFolder.exists()) {
                        //目录未创建 没有解压过
                        outputFolder.mkdirs();
                        unZip(obbFile, outputFolder.getAbsolutePath());
                    } else {
                        //目录已创建 判断是否解压过
                        if (outputFolder.listFiles() == null) {
                            //解压过的文件被删除
                            unZip(obbFile, outputFolder.getAbsolutePath());
                        } else {
                            //此处可添加文件对比逻辑
                            Log.d("bbt", "unZipObb error : outputFolder.listFiles() != null");
                        }
                    }
                }
            }
        } catch (Exception e) {
            Log.d("bbt", "unZipObb error : Exception");
            e.printStackTrace();
        }

Android SurfaceView是一个轻量级的视图组件,主要用于绘制自定义图形,而AAR (Android Archive) 是Android的库模块打包格式,可以包含源代码、资源以及编译后的二进制文件OBB (Optimized BZip2 Bundle) 是一种用于存储大容量数据(如游戏地图、高清资源等)的技术,因为它们超过了APK大小限制。 如果你想在SurfaceView中使用AAR库并加载OBB数据,你需要做以下几步: 1. **添加依赖**:将AAR库添加到项目级别的build.gradle文件的dependencies中,并设置适当的aar和obb标签。 ```groovy implementation 'com.example.library:aar-name:version' obb('com.example.library:obb-name:version') ``` 2. **配置Manifest文件**:在AndroidManifest.xml中,声明支持外部文件访问权限,并提供一个ACTION机制来引用OBB文件。 ```xml <application> <activity ...> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/vnd.android.package-archive" /> </intent-filter> </activity> </application> ``` 3. **处理OBB文件**:通常需要第三方库(比如Universal-Loader、Picasso等)配合,通过`getExternalFilesDir("obb")`获取OBB数据的路径,然后解压OBB内容到合适的位置。 4. **在SurfaceView中使用数据**:在SurfaceView的生命周期管理中,确保在适当的时候读取并处理OBB文件中的资源,将其展示在视图上。 5. **启动时检查和加载**:在应用程序启动时,检查OBB是否已安装并完整,如果没有则提示用户下载或自动下载。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥游侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值