java 解析apk、ipa文件 获得app信息

	package commonutil;

	import java.io.ByteArrayInputStream;
	import java.io.ByteArrayOutputStream;
	import java.io.FileNotFoundException;
	import java.io.IOException;
	import java.io.InputStream;
	import java.util.HashMap;
	import java.util.Map;
	import java.util.zip.ZipEntry;
	import java.util.zip.ZipException;
	import java.util.zip.ZipInputStream;

	import org.apkinfo.api.util.AXmlResourceParser;
	import org.apkinfo.api.util.XmlPullParser;
	import org.apkinfo.api.util.XmlPullParserException;

	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应用程序包,获得包名、应用程序名、版本号等信息。
	 *  
	 * @author Eric.Sung
	 * @date 2015.2.10
	 *
	 */
	public class PackageUtil {
		/**
		 * 解析IPA文件
		 * 
		 * @author Eric.Sung
		 * @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.contains("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;
		}
		
		/**
		 *  解析APK文件
		 *  
		 *  @author Eric.Sung
		 *  @date 2015.2.10
		 *  @param inputstream
		 *  @return Map<String,Map<String,Object>>
		 */
		public static Map<String,Map<String,Object>> analysiApk(InputStream is){
			// result
			Map<String,Map<String,Object>> resultMap = new HashMap<String,Map<String,Object>>();
			try {
				ZipInputStream zipIns = new ZipInputStream(is);
				zipIns.getNextEntry();
				AXmlResourceParser parser = new AXmlResourceParser();
				parser.open(zipIns);
				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> manifest = resultMap.get("MANIFEST");
	                    	Map<String,Object> application = resultMap.get("APPLICATION");
	                    	if(manifest!=null&&application!=null){
	                    		flag = false;
	                    	}
	                    	continue;
	                    }
	                }
				}
			} catch (ZipException 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);
			} catch (IOException 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);
			} catch (XmlPullParserException 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;
		}
		
		/**
		 * Test ipa
		 * 
		 * @author Eric.Sung
		 * @date 2015.2.10
		 */
		/*public static void TestIpa(){
			try {
				File file = new File("D:/com.mojang.minecraftpe_0.10.4.ipa");
				InputStream is = new FileInputStream(file);
				Map<String,Object> result = analysiIpa(is);
				System.out.println(result);
				is.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}*/
		
		/**
		 * Test apk
		 * 
		 * @author Eric.Sung
		 * @date 2015.2.10
		 */
		/*public static void TestApk(){
			File file = new File("D:\\baidu.apk");
			try {
				FileInputStream fis = new FileInputStream(file);
				Map<String,Map<String,Object>> result = analysiApk(fis);
				System.out.println(result.toString());
				fis.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}*/
	}

转载于:https://my.oschina.net/aoaoH/blog/393955

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值