解析apk、ipa应用程序包,获得包名、应用程序名、版本号等信息。

package com.shark.apptool.server.utils;


import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStream;

import java.util.HashMap;

import java.util.Map;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipInputStream;


import org.apkinfo.api.util.AXmlResourceParser;

import org.apkinfo.api.util.XmlPullParser;


import com.dd.plist.NSDictionary;

import com.dd.plist.NSNumber;

import com.dd.plist.NSObject;

import com.dd.plist.NSString;

import com.dd.plist.PropertyListParser;


/**

 * 解析apk、ipa应用程序包,获得包名、应用程序名、版本号等信息。

 * @date 2015.2.10

 * 

 */

public class PackageUtil {

/**

* 解析IPA文件

* @date 2015.2.10

*/

public static Map<String, Object> analysiIpa(InputStream is) {

// result map

Map<String, Object> resultMap = new HashMap<String, Object>();

try {

ZipInputStream zipIns = new ZipInputStream(is);

ZipEntry ze;

InputStream infoIs = null;

while ((ze = zipIns.getNextEntry()) != null) {

if (!ze.isDirectory()) {

String name = ze.getName();

if (name.endsWith(".app/Info.plist")) {

ByteArrayOutputStream _copy = new ByteArrayOutputStream();

// int read = 0;

int chunk = 0;

byte[] data = new byte[256];

while (-1 != (chunk = zipIns.read(data))) {

// read += data.length;

_copy.write(data, 0, chunk);

}

infoIs = new ByteArrayInputStream(_copy.toByteArray());

break;

}

}

}

NSDictionary rootDict = (NSDictionary) PropertyListParser

.parse(infoIs);

String[] keyArray = rootDict.allKeys();

for (String key : keyArray) {

NSObject value = rootDict.objectForKey(key);

if (key.equals("CFBundleSignature")) {

continue;

}

if (value.getClass().equals(NSString.class)

|| value.getClass().equals(NSNumber.class)) {

resultMap.put(key, value.toString());

}

}

zipIns.close();

} catch (FileNotFoundException e) {

resultMap.put("error", e.getStackTrace());

} catch (Exception e) {

resultMap.put("error", e.getStackTrace());

}

return resultMap;

}


/***

* @param apkFile

*            apk文件

* @return

*/

@SuppressWarnings({ "resource", "unchecked" })

public static Map<String, Object> analysiApk(File apkFile) {

ZipFile zipFile;

Map<String, Object> resultMap = new HashMap<String, Object>();

try {

zipFile = new ZipFile(apkFile);

ZipEntry zipEntry = zipFile.getEntry(("AndroidManifest.xml"));

AXmlResourceParser parser = new AXmlResourceParser();

parser.open(zipFile.getInputStream(zipEntry));

boolean flag = true;

while (flag) {

int type = parser.next();

if (type == XmlPullParser.START_TAG) {


int count = parser.getAttributeCount();

String action = parser.getName().toUpperCase();

if (action.equals("MANIFEST")

|| action.equals("APPLICATION")) {

Map<String, Object> tempMap = new HashMap<String, Object>();

for (int i = 0; i < count; i++) {

String name = parser.getAttributeName(i);

String value = parser.getAttributeValue(i);

value = value == null ? "" : value;

tempMap.put(name, value);

}

resultMap.put(action, tempMap);

} else {

Map<String, Object> application = (Map<String, Object>) resultMap

.get("APPLICATION");

Map<String, Object> manifest = (Map<String, Object>) resultMap

.get("MANIFEST");

if (manifest != null && application != null) {

flag = false;

}

continue;

}

}

}// end while

} catch (Exception e) {

Map<String, Object> errorMap = new HashMap<String, Object>();

errorMap.put("cause", e.getCause());

errorMap.put("message", e.getMessage());

errorMap.put("stack", e.getStackTrace());

resultMap.put("error", errorMap);

}

return resultMap;

}



 

 

}


转载于:https://my.oschina.net/zhougui/blog/523958

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值