日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java读取 info.plist源码_Java 解析 IPA 文件,读取 Info.plist 信息-Go语言中文社区

發布時間:2025/4/5 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java读取 info.plist源码_Java 解析 IPA 文件,读取 Info.plist 信息-Go语言中文社区 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在做移動MDM功能的時候,就遇到了這樣一個問題,當用戶上傳IPA文件時,我如何知道這個IPA文件的相關信息呢?IPA文件有一個很重要的文件Info.plist 就類似于Android程序的Manifest.xml,只要能夠從IPA文件中提取出來Info.plist,然后在進行解析提起相關信息,那就馬到成功了。那么,按照上面步驟開始了。

1、Java解壓IPA文件,只獲取 Info.plist

/**

*?解壓IPA文件,只獲取IPA文件的Info.plist文件存儲指定位置

*?@param?file

*?zip文件

*?@param?unzipDirectory

*?解壓到的目錄

*?@throws?Exception

*/

private?static?File?getZipInfo(File?file,?String?unzipDirectory)

throws?Exception?{

//?定義輸入輸出流對象

InputStream?input?=?null;

OutputStream?output?=?null;

File?result?=?null;

File?unzipFile?=?null;

ZipFile?zipFile?=?null;

try?{

//?創建zip文件對象

zipFile?=?new?ZipFile(file);

//?創建本zip文件解壓目錄

String?name?=?file.getName().substring(0,file.getName().lastIndexOf("."));

unzipFile?=?new?File(unzipDirectory?+?"/"?+?name);

if?(unzipFile.exists()){

unzipFile.delete();

}

unzipFile.mkdir();

//?得到zip文件條目枚舉對象

Enumeration?zipEnum?=?zipFile.getEntries();

//?定義對象

ZipEntry?entry?=?null;

String?entryName?=?null;

String?names[]?=?null;

int?length;

//?循環讀取條目

while?(zipEnum.hasMoreElements())?{

//?得到當前條目

entry?=?zipEnum.nextElement();

entryName?=?new?String(entry.getName());

//?用/分隔條目名稱

names?=?entryName.split("\/");

length?=?names.length;

for?(int?v?=?0;?v?

if(entryName.endsWith(".app/Info.plist")){?//?為Info.plist文件,則輸出到文件

input?=?zipFile.getInputStream(entry);

result?=?new?File(unzipFile.getAbsolutePath()+?"/Info.plist");

output?=?new?FileOutputStream(result);

byte[]?buffer?=?new?byte[1024?*?8];

int?readLen?=?0;

while?((readLen?=?input.read(buffer,?0,?1024?*?8))?!=?-1){

output.write(buffer,?0,?readLen);

}

break;

}

}

}

}?catch?(Exception?ex)?{

ex.printStackTrace();

}?finally?{

if?(input?!=?null)

input.close();

if?(output?!=?null)?{

output.flush();

output.close();

}

//?必須關流,否則文件無法刪除

if(zipFile?!=?null){

zipFile.close();

}

}

//?如果有必要刪除多余的文件

if(file.exists()){

file.delete();

}

return?result;

}

/**

*?IPA文件的拷貝,把一個IPA文件復制為Zip文件,同時返回Info.plist文件

*?參數?oldfile?為?IPA文件

*/

private?static?File?getIpaInfo(File?oldfile)?throws?IOException?{

try{

int?byteread?=?0;

String?filename?=?oldfile.getAbsolutePath().replaceAll(".ipa",?".zip");

File?newfile?=?new?File(filename);

if?(oldfile.exists()){

//?創建一個Zip文件

InputStream?inStream?=?new?FileInputStream(oldfile);

FileOutputStream?fs?=?new?FileOutputStream(newfile);

byte[]?buffer?=?new?byte[1444];

while?((byteread?=?inStream.read(buffer))?!=?-1){

fs.write(buffer,0,byteread);

}

if(inStream?!=?null){

inStream.close();

}

if(fs?!=?null){

fs.close();

}

//?解析Zip文件

return?unzip(newfile,?newfile.getParent());

}

}catch(Exception?e){

e.printStackTrace();

}

return?null;

}

2、Java讀取Info.plist文件,獲取需要的信息

/**

*?通過IPA文件獲取Info信息

*?這個方法可以重構,原因是指獲取了部分重要信息,如果想要獲取全部,那么應該返回一個Map

*?對于plist文件中的數組信息應該序列化存儲在Map中,那么只需要第三發jar提供的NSArray可以做到!

*/

public?static?Map?getIpaInfoMap(File?ipa)?throws?Exception{

Map?map?=?new?HashMap();

File?file?=?getIpaInfo(ipa);

//?第三方jar包提供

NSDictionary?rootDict?=?(NSDictionary)?PropertyListParser.parse(file);

//?應用包名

NSString?parameters?=?(NSString)?rootDict.objectForKey("CFBundleIdentifier");

map.put("CFBundleIdentifier",?parameters.toString());

//?應用名稱

parameters?=?(NSString)?rootDict.objectForKey("CFBundleName");

map.put("CFBundleName",?parameters.toString());

//?應用版本

parameters?=?(NSString)?rootDict.objectForKey("CFBundleVersion");

map.put("CFBundleVersion",?parameters.toString());

//?應用展示的名稱

parameters?=?(NSString)?rootDict.objectForKey("CFBundleDisplayName");

map.put("CFBundleDisplayName",?parameters.toString());

//?應用所需IOS最低版本

parameters?=?(NSString)?rootDict.objectForKey("MinimumOSVersion");

map.put("MinimumOSVersion",?parameters.toString());

//?如果有必要,應該刪除解壓的結果文件

file.delete();

file.getParentFile().delete();

return?map;

}

3、程序測試

public?static?void?main(String[]?args)?throws?Exception?{

File?file?=?new?File("d:/UniAccess.ipa");

Map?map?=?getIpaInfoMap(file);

for(String?key?:?map.keySet()){

System.out.println(key+"?:?"+map.get(key));

}

}

4、測試結果

CFBundleIdentifier?:?com.qihoo.installSafety

CFBundleDisplayName?:?360手機衛士

CFBundleName?:?360MobileSafe

CFBundleVersion?:?4.2.0.2

MinimumOSVersion?:?5.0

5、相關jar包,以及IPA文件,可以到如下指定地址下載

1)http://download.csdn.net/detail/wp562846864/8474481

2)http://m1.app111.org/2014/09/19/20140919142959.ipa

總結

以上是生活随笔為你收集整理的java读取 info.plist源码_Java 解析 IPA 文件,读取 Info.plist 信息-Go语言中文社区的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。