Java 使用apk-parser 解析apk文件

本文介绍如何使用APK-parse脚手架解析上传的APK或IPA文件,提取关键信息如版本、名称和大小,适用于移动端和应用商店验证。

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

apk-parse脚手架

apk-parse脚手架链接: 插件下载地址


<dependency>
    <groupId>net.dongliu</groupId>
    <artifactId>apk-parser</artifactId>
    <version>2.6.10</version>
</dependency>

废话不多说直接上代码 这是最早的写法

public R apkParser(@RequestParam("file") MultipartFile multipartFile){
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件后缀
        String prefix=fileName.substring(fileName.lastIndexOf("."));
        if (!prefix.equals(".apk")){
            return R.error("请上传.apk文件");
        }
        Map<String, Object> map = new HashMap<>();
        try {
            File excelFile = new File(fileName);
            FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), excelFile);
            ApkFile apkFile = new ApkFile(excelFile);
            ApkMeta apkMeta = apkFile.getApkMeta();
            map.put("appName", apkMeta.getLabel());
            map.put("appId", apkMeta.getPackageName());
            map.put("versionCode", apkMeta.getVersionCode());
            map.put("versionName", apkMeta.getVersionName());
            map.put("sice",(double) (excelFile.length() * 100 / 1024 / 1024) / 100 + " MB");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(map);

    }

封装工具类

import com.dd.plist.NSDictionary;
import com.dd.plist.NSString;
import com.dd.plist.PropertyListParser;
import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ApkAndIpaUtil {

    /**
     * 解析Apk文件
     *
     * @param file Apk的文件路径
     * @return
     */
    public static Map<String, Object> parseApk(File file) {
        Map<String, Object> map = new HashMap();
        try {
            // File file = new File(url);
            ApkFile apkFile = new ApkFile(file);
            ApkMeta apkMeta = apkFile.getApkMeta();
            map.put("appName", apkMeta.getLabel());
            map.put("appId", apkMeta.getPackageName());
            map.put("versionCode", apkMeta.getVersionCode());
            map.put("versionName", apkMeta.getVersionName());
            map.put("packetSize", (double) (file.length() * 100 / 1024 / 1024) / 100 + " MB");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 解析ipa文件
     *
     * @param file ipa的文件路径
     * @return
     */
    public static Map<String, Object> parseIpa(File file) {
        Map<String, Object> map = new HashMap();
        try {
            // File file = new File(url);
            InputStream is = new FileInputStream(file);
            ZipInputStream zipIns = new ZipInputStream(is);
            ZipEntry ze;
            InputStream infoIs = null;
            while ((ze = zipIns.getNextEntry()) != null) {
                if (!ze.isDirectory()) {
                    String name = ze.getName();
                    if (null != name && name.toLowerCase().contains(".app/info.plist")) {
                        ByteArrayOutputStream _copy = new ByteArrayOutputStream();
                        int chunk = 0;
                        byte[] data = new byte[1024];
                        while (-1 != (chunk = zipIns.read(data))) {
                            _copy.write(data, 0, chunk);
                        }
                        infoIs = new ByteArrayInputStream(_copy.toByteArray());
                        break;
                    }
                }
            }

            NSDictionary rootDict = (NSDictionary) PropertyListParser.parse(infoIs);

            //如果想要查看有哪些key ,可以把下面注释放开
//            for (String keyName : rootDict.allKeys()) {
//                System.out.println(keyName + ":" + rootDict.get(keyName).toString());
//            }

            // 应用包名CFBundleDisplayNameCFBundleIdentifier
            NSString parameters = (NSString) rootDict.get("CFBundleIdentifier");
            map.put("appId", parameters.toString());
            // 应用版本名
            parameters = (NSString) rootDict.objectForKey("CFBundleShortVersionString");
            map.put("versionName", parameters.toString());
            // 应用版本号
            parameters = (NSString) rootDict.get("CFBundleVersion");
            map.put("versionCode", parameters.toString());
            parameters = (NSString) rootDict.get("CFBundleDisplayName");
            map.put("appName", parameters.toString());
            map.put("packetSize", (double) (file.length() * 100 / 1024 / 1024) / 100 + " MB");
            infoIs.close();
            is.close();
            zipIns.close();

        } catch (Exception e) {
            System.out.println("ipa包解析异常!");
        }
        return map;
    }

    /**
     * String url = "https://img.cheguakao.com/prodimg/20200927/__UNI__99584F9_0106120442.ipa";
     * 网络资源下载到项目
     *
     * @param url
     * @return
     */
    public static File openStream(String url) {
        //对本地文件命名
        String fileName = url.substring(url.lastIndexOf("."), url.length());
        File file = null;
        URL urlfile;
        InputStream inStream = null;
        OutputStream os = null;
        try {
            file = File.createTempFile("net_url", fileName);
            //下载
            urlfile = new URL(url);
            inStream = urlfile.openStream();
            os = new FileOutputStream(file);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
                if (null != inStream) {
                    inStream.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //可返回File  对象  这里只返回文件的路径
        return file;
    }

}

调用信息

public R apkParser(String url) {
        // 获取文件名
        String fileName = url.substring(url.lastIndexOf("."), url.length());
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        File file = ApkAndIpaUtil.openStream(url);
        Map<String, Object> map = null;
        if (prefix.equals(".apk")) {
            map = ApkAndIpaUtil.parseApk(file);
        } else if (prefix.equals(".ipa")) {
            map = ApkAndIpaUtil.parseIpa(file);
        } else {
            return R.error("请上传.apk /.ipa文件");
        }
        // 判断是否存在,存在删除
        if (file.exists()) {
            file.delete();
        }
        return R.ok(map);
    }
我们使用的是oss 在pc直接上传到oss之后返回的url去做解析
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值