当前位置: 首页 > news >正文

apk-parse包信息解析

最近公司做项目,需要解析apk包的基本信息,上网找了好多资料,最终决定使用apk-parse。

.yml文件

引入jar包

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

解析操作代码

/**
* 解析apk
* @param apkFile 文件
* @return
* @throws IOException
*/

public ApkParseVo parseApk(MultipartFile apkFile){
File tempFile=null;
ApkParseVo apkParseVo=new ApkParseVo();
if (ObjectUtil.isNull(apkFile)) {
throw new ServiceException("上传文件不能为空");
}
// 获取后缀名
String suffix = apkFile.getOriginalFilename().substring(apkFile.getOriginalFilename().lastIndexOf(".")+1);
if(StringUtils.isBlank(suffix)){
throw new ServiceException("后缀名不能为空");
}
// 后缀名是apk 解析信息
else if(apkSuffix.equals(suffix)){
ApkMeta apkMeta=null;
try{
// 临时文件
tempFile =new File(tempPath+apkSuffix+"\\"+new Date().getTime()+"."+suffix);
try(FileOutputStream fileOutputStream = new FileOutputStream(tempFile)) {
fileOutputStream.write(apkFile.getBytes());
} catch (IOException e) {
throw new ServiceException("输出错误");
}
try (ApkFile apk = new ApkFile(tempFile)) {
apkMeta=apk.getApkMeta();
} catch (Exception e) {

}
}catch (Exception e){
throw new ServiceException("出错了");
}
if(ObjectUtil.isNull(apkMeta)){
throw new ServiceException("出错了");
}

// 输出解析信息
System.out.println(PlatformEnum.ANDROID.getCode());
System.out.println(apkMeta.getLabel());
System.out.println(apkMeta.getVersionName());
System.out.println(apkMeta.getVersionCode().toString());
System.out.println(apkMeta.getPackageName());
// 上传图片
String icons=apkMeta.getIcon();
if(StringUtils.isNotBlank(icons)){
// 这里传参的packageName是在resource目录下创建的文件名,具体的可以灵活修改
File iconFile=new File(saveBit(icons, tempFile));


// 删除文件
FileUtils.del(iconFile);
}
}
// 删除apk临时文件
FileUtils.del(tempFile);
return apkParseVo;
}

解析APK中的图片

/**
* 解析apk, 拷贝图标到指定url文件的目录下
*
* @param apkIconUrl apk的iconUrl
* @param tempFile apk临时文件
*/
public String saveBit(String apkIconUrl, File tempFile) {
ZipFile zf=null;
// 输入流
InputStream in=null;
ZipInputStream zin = null;
InputStream inStream =null;
FileOutputStream fileOutStream=null;
ByteArrayOutputStream outStream=null;
// 临时存放路径
String iconPath=tempPath+iconTemp+"\\" + File.separator + new Date().getTime()+".png";
try {
// 访问apk 里面的文件
zf = new ZipFile(tempFile);
in = new BufferedInputStream(new FileInputStream(tempFile));
zin = new ZipInputStream(in);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
if (ze.getName().equals(apkIconUrl)) {
inStream = zf.getInputStream(ze);
outStream = new ByteArrayOutputStream();
// 创建一个Buffer字符串
byte[] buffer = new byte[1024];
// 每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
// 使用一个输入流从buffer里把数据读取出来
while ((len = inStream.read(buffer)) != -1) {
// 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
// 把outStream里的数据写入内存
// 得到图片的二进制数据,以二进制封装得到数据,具有通用性
byte[] data = outStream.toByteArray();
// new一个文件对象用来保存图片,默认保存当前工程根目录
File imageFile = new File(iconPath);
// 创建输出流
fileOutStream = new FileOutputStream(imageFile);
// 写入数据
fileOutStream.write(data);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭输入流
assert zf != null;
zf.close();
assert in != null;
in.close();
assert zin != null;
zin.close();
assert inStream != null;
inStream.close();
assert outStream != null;
outStream.close();
assert fileOutStream != null;
fileOutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return iconPath;
}

http://www.lryc.cn/news/350659.html

相关文章:

  • AI绘画进阶工具ComfyUI 傻瓜整合包安装教程!模型共享,一键安装!
  • 无人机摄影测量数据处理、三维建模及在土方量计算
  • 大模型平台后端开发(xiaomi)
  • 性能测试工具—jmeter的基础使用
  • 前端 JS 经典:CommonJs 规范
  • 三分钟速览量化交易系统:揭秘QMT与Ptrade(内附免费提供渠道)
  • 处理QTcpSocket接收到数据的槽函数
  • 回归的无分布预测推理
  • 有限域中的一些概念
  • 使用css的box-reflect属性制作倒影效果
  • ChatGPT 4o 使用案例之一
  • 【免费Web系列】大家好 ,今天是Web课程的第一天点赞收藏关注,持续更新作品 !
  • C++|树形关联式容器(set、map、multiset、multimap)介绍使用
  • springboot整合s3,用ImageIO进行图片格式转换
  • Windows 10无法远程桌面连接:原因及解决方案
  • 图神经网络实战(10)——归纳学习
  • Python——IO编程
  • 什么是网络端口?为什么会有高危端口?
  • CleanMyMac X v4.14.6中文破解版,让您的电脑像新的一样
  • LeetCode 235. 二叉搜索树的最近公共祖先
  • 基于ASN.1的RSA算法公私钥存储格式解读
  • RS2227XN功能和参数介绍及PDF资料
  • 机器人非线性阻抗控制系统
  • pandas style添加表格边框,或是只添加下边框等自定义边框样式设置
  • OpenHarmony 3GPP协议开发深度剖析——一文读懂RIL
  • windows部署腾讯tmagic-editor02-Runtime
  • “分块”算法的基本要素及 build() 函数的构建细节
  • 畅捷通TPlus keyEdit.aspx、KeyInfoList.aspx SQL注入漏洞复现
  • Ubuntu22 下配置 Qt5 环境
  • 普通人也能创业!轻资产短视频带货项目,引领普通人实现创业梦想