解析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

发布了150 篇原创文章 · 获赞 52 · 访问量 19万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览