原文:http://www.33hot.com/thread-27414-1-1.html
一、读取ipa文件配置信息(简单的),
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
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.ZipInputStream;
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;
/**
* 读取ipa
* @param file
* @return
* @throws IOException
*/
public class IpaUtil {
//public static final
public static Map<String, Object> analysiIpa(File file) throws IOException
{
InputStream is = new FileInputStream(file);
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());
}
is.close();
return resultMap;
}
}
二、读取apk文件配置信息(详细)
import org.xmlpull.v1.XmlPullParser;
import android.content.res.AXmlResourceParser;
import android.util.TypedValue;
public class ApkUtil {
private final static String MainXmlName = "AndroidManifest.xml";
private static final float RADIX_MULTS[] = { 0.00390625F, 3.051758E-005F, 1.192093E-007F, 4.656613E-010F };
private static final String DIMENSION_UNITS[] = { "px", "dip", "sp", "pt", "in", "mm", "", "" };
private static final String FRACTION_UNITS[] = { "%", "%p", "", "", "", "", "", "" };
public static String[] analysisApk(File file) throws Exception
{
String [] strs = new String[3];
ZipFile zipFile = new ZipFile(file);
Enumeration<?> enumeration = zipFile.entries();
ZipEntry zipEntry = null;
while (enumeration.hasMoreElements())
{
zipEntry = (ZipEntry) enumeration.nextElement();
if(MainXmlName.equals(zipEntry.getName())){
AXmlResourceParser parser = new AXmlResourceParser();
parser.open(zipFile.getInputStream(zipEntry));
while (true) {
int type = parser.next();
if (type == XmlPullParser.END_DOCUMENT) {
break;
}
switch (type) {
case XmlPullParser.START_TAG: {
for (int i = 0; i != parser.getAttributeCount(); ++i) {
if("versionCode".equals(parser.getAttributeName(i))){
strs[0] = getAttributeValue(parser, i);
} else if ("versionName".equals(parser.getAttributeName(i))) {
strs[1] = getAttributeValue(parser, i);
} else if ("package".equals(parser.getAttributeName(i))) {
strs[2] = getAttributeValue(parser, i);
}
}
}
}
}
}
}
zipFile.close();
return strs;
}
private static String getAttributeValue(AXmlResourceParser parser, int index) {
int type = parser.getAttributeValueType(index);
int data = parser.getAttributeValueData(index);
if (type == TypedValue.TYPE_STRING) {
return parser.getAttributeValue(index);
}
if (type == TypedValue.TYPE_ATTRIBUTE) {
return String.format("?%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_REFERENCE) {
return String.format("@%s%08X", getPackage(data), data);
}
if (type == TypedValue.TYPE_FLOAT) {
return String.valueOf(Float.intBitsToFloat(data));
}
if (type == TypedValue.TYPE_INT_HEX) {
return String.format("0x%08X", data);
}
if (type == TypedValue.TYPE_INT_BOOLEAN) {
return data != 0 ? "true" : "false";
}
if (type == TypedValue.TYPE_DIMENSION) {
return Float.toString(complexToFloat(data)) + DIMENSION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type == TypedValue.TYPE_FRACTION) {
return Float.toString(complexToFloat(data)) + FRACTION_UNITS[data & TypedValue.COMPLEX_UNIT_MASK];
}
if (type >= TypedValue.TYPE_FIRST_COLOR_INT && type <= TypedValue.TYPE_LAST_COLOR_INT) {
return String.format("#%08X", data);
}
if (type >= TypedValue.TYPE_FIRST_INT && type <= TypedValue.TYPE_LAST_INT) {
return String.valueOf(data);
}
return String.format("<0x%X, type 0x%02X>", data, type);
}
private static String getPackage(int id) {
if (id >>> 24 == 1) {
return "android:";
}
return "";
}
// / ILLEGAL STUFF, DONT LOOK :)
public static float complexToFloat(int complex) {
return (float) (complex & 0xFFFFFF00) * RADIX_MULTS[(complex >> 4) & 3];
}
}