public class IpaUtil {
public static Map<String, Object> readIPA(String ipaURL, String picturepath) {
Map<String, Object> map = new HashMap<String, Object>();
String newIconName = null;
try {
File file = new File(ipaURL);
InputStream is = new FileInputStream(file);
InputStream is2 = new FileInputStream(file);
ZipInputStream zipIns = new ZipInputStream(is);
ZipInputStream zipIns2 = new ZipInputStream(is2);
ZipEntry ze;
ZipEntry ze2;
InputStream infoIs = null;
NSDictionary rootDict = null;
String icon = null;
while ((ze = zipIns.getNextEntry()) != null) {
if (!ze.isDirectory()) {
String name = ze.getName();
if (null != name && name.toLowerCase().contains(".app/info.plist")) {
ByteArrayOutputStream _copy = new ByteArrayOutputStream();
int chunk = 0;
byte[] data = new byte[1024];
while (-1 != (chunk = zipIns.read(data))) {
_copy.write(data, 0, chunk);
}
infoIs = new ByteArrayInputStream(_copy.toByteArray());
rootDict = (NSDictionary)PropertyListParser.parse(infoIs);
NSDictionary iconDict = (NSDictionary)rootDict.get("CFBundleIcons");
if (rootDict.containsKey("CFBundleIconFiles")) {
NSArray CFBundleIconFiles = (NSArray)rootDict.get("CFBundleIconFiles");
NSObject[] array = CFBundleIconFiles.getArray();
if (!Utils.isArrayEmpty(array)) {
icon = CFBundleIconFiles.getArray()[array.length - 1].toString();
icon = icon.replace(".png", "");
if (null != icon) {
break;
}
}
}
// 獲取圖標名稱
while (null != iconDict) {
if (iconDict.containsKey("CFBundlePrimaryIcon")) {
NSDictionary CFBundlePrimaryIcon = (NSDictionary)iconDict.get("CFBundlePrimaryIcon");
if (CFBundlePrimaryIcon.containsKey("CFBundleIconFiles")) {
NSArray CFBundleIconFiles = (NSArray)CFBundlePrimaryIcon.get("CFBundleIconFiles");
NSObject[] array = CFBundleIconFiles.getArray();
if (!Utils.isArrayEmpty(array)) {
icon = CFBundleIconFiles.getArray()[array.length - 1].toString();
icon = icon.replace(".png", "");
if (null != icon) {
break;
}
}
}
if (CFBundlePrimaryIcon.containsKey("CFBundleIconFiles~ipad")) {
NSArray CFBundleIconFiles =
(NSArray)CFBundlePrimaryIcon.get("CFBundleIconFiles~ipad");
NSObject[] array = CFBundleIconFiles.getArray();
if (!Utils.isArrayEmpty(array)) {
icon = CFBundleIconFiles.getArray()[array.length - 1].toString();
icon = icon.replace(".png", "");
if (null != icon) {
break;
}
}
}
}
if (iconDict.containsKey("CFBundleIconFiles")) {
NSArray iconArray = (NSArray)rootDict.get("CFBundleIconFiles");
if (null != iconArray) {
NSObject[] array1 = iconArray.getArray();
if (!Utils.isArrayEmpty(array1)) {
icon = array1[array1.length - 1].toString();
icon = icon.replace(".png", "");
if (null != icon) {
break;
}
}
}
}
}
break;
}
}
}
if (null != icon) {
// 根據圖標名稱下載圖標文件到指定位置
while ((ze2 = zipIns2.getNextEntry()) != null) {
if (!ze2.isDirectory()) {
String name = ze2.getName();
if (name.contains(icon.trim())) {
FileOutputStream fos = new FileOutputStream(new File(picturepath));
int chunk = 0;
byte[] data = new byte[1024];
while (-1 != (chunk = zipIns2.read(data))) {
fos.write(data, 0, chunk);
}
fos.close();
}
}
}
}
if (!Utils.fileIsExist(picturepath)) {// 未解析出
loop:
while ((ze2 = zipIns2.getNextEntry()) != null) {
if (!ze2.isDirectory()) {
String name = ze2.getName();
if (name.contains("AppIcon60x60@3x".trim())) {
FileOutputStream fos = new FileOutputStream(new File(picturepath));
int chunk = 0;
byte[] data = new byte[1024];
while (-1 != (chunk = zipIns2.read(data))) {
fos.write(data, 0, chunk);
}
fos.close();
break loop;
} else if (name.contains("AppIcon60x60@2x".trim())) {
FileOutputStream fos3 = new FileOutputStream(new File(picturepath));
int chunk3 = 0;
byte[] data3 = new byte[1024];
while (-1 != (chunk3 = zipIns2.read(data3))) {
fos3.write(data3, 0, chunk3);
}
fos3.close();
break loop;
} else if (name.contains("AppIcon76x76@2x~ipad".trim())) {
FileOutputStream fos3 = new FileOutputStream(new File(picturepath));
int chunk3 = 0;
byte[] data3 = new byte[1024];
while (-1 != (chunk3 = zipIns2.read(data3))) {
fos3.write(data3, 0, chunk3);
}
fos3.close();
break loop;
}
}
}
}
// 應用包名
NSString parameters = (NSString)rootDict.get("CFBundleIdentifier");
if (null != parameters) {
map.put("package", parameters.toString());
}
// 獲取urlSchemes
NSArray urlTypes = (NSArray)rootDict.get("CFBundleURLTypes");
/* if (null != urlTypes) {
NSObject[] array = urlTypes.getArray();
if (!Utils.isArrayEmpty(array)) {
if (null != array[0]) {
NSArray urlt = (NSArray)((NSDictionary)array[0]).get("CFBundleURLSchemes");
NSObject[] urlArray = urlt.getArray();
if (!Utils.isArrayEmpty(urlArray)) {
if (null != urlArray[0]) {
parameters = (NSString)urlArray[0];
map.put("urlSchemes", parameters.toString());
}
}else{
map.put("urlSchemes", null);
}
}
}else{
map.put("urlSchemes", null);
}
} else {
map.put("urlSchemes", null);
}
// 應用程序名稱
parameters = (NSString)rootDict.objectForKey("CFBundleName");
if (null != parameters) {
map.put("displayName", parameters.toString());
}
parameters = (NSString)rootDict.objectForKey("CFBundleDisplayName");
if (null != parameters) {
map.put("displayName", parameters.toString());
}
// 應用版本名
parameters = (NSString)rootDict.objectForKey("CFBundleShortVersionString");
if (null != parameters) {
map.put("versionName", parameters.toString());
}
// 應用版本號
parameters = (NSString)rootDict.get("CFBundleVersion");
if (null != parameters) {
map.put("versionCode", parameters.toString());
}
infoIs.close();
is.close();
zipIns.close();
} catch (Exception e) {
e.printStackTrace();
map.put("code", "fail");
map.put("error", "讀取ipa文件失敗");
}
// 此腳本用來還原軟件圖標
Process process;
/*try {
process = Runtime.getRuntime().exec("sh /www/server/upload/img/ipin.sh");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}*/
map.put("icon", newIconName);
map.put("code", "success");
return map;
}
//測試解析
public static void main(String[] args) {
IpaUtil ipa = new IpaUtil();
Map<String,Object> mapIpa = ipa.readIPA("D://google//新建文件夾//異常//Payload.ipa", "D://google//新建文件夾//hyd.png//");
for (String key : mapIpa.keySet()) {
System.out.println(key + ":" + mapIpa.get(key));
}
}
}
需要引入dd-plist-1.21.jar
下載地址https://download.csdn.net/download/weixin_42286461/11004227