安装maven 插件
<!-- https://mvnrepository.com/artifact/com.googlecode.plist/dd-plist -->
<dependency>
<groupId>com.googlecode.plist</groupId>
<artifactId>dd-plist</artifactId>
<version>1.23</version>
</dependency>
上代码
import com.dd.plist.NSDictionary;
import com.dd.plist.NSString;
import com.dd.plist.PropertyListParser;
import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ApkAndIpaUtil {
public static Map<String, Object> parseApk(File file) {
Map<String, Object> map = new HashMap();
try {
ApkFile apkFile = new ApkFile(file);
ApkMeta apkMeta = apkFile.getApkMeta();
map.put("appName", apkMeta.getLabel());
map.put("appId", apkMeta.getPackageName());
map.put("versionCode", apkMeta.getVersionCode());
map.put("versionName", apkMeta.getVersionName());
map.put("packetSize", (double) (file.length() * 100 / 1024 / 1024) / 100 + " MB");
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
public static Map<String, Object> parseIpa(File file) {
Map<String, Object> map = new HashMap();
try {
InputStream is = new FileInputStream(file);
ZipInputStream zipIns = new ZipInputStream(is);
ZipEntry ze;
InputStream infoIs = 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());
break;
}
}
}
NSDictionary rootDict = (NSDictionary) PropertyListParser.parse(infoIs);
for (String keyName : rootDict.allKeys()) {
System.out.println(keyName + ":" + rootDict.get(keyName).toString());
}
NSString parameters = (NSString) rootDict.get("CFBundleIdentifier");
map.put("appId", parameters.toString());
parameters = (NSString) rootDict.objectForKey("CFBundleShortVersionString");
map.put("versionName", parameters.toString());
parameters = (NSString) rootDict.get("CFBundleVersion");
map.put("versionCode", parameters.toString());
parameters = (NSString) rootDict.get("CFBundleDisplayName");
map.put("appName", parameters.toString());
map.put("packetSize", (double) (file.length() * 100 / 1024 / 1024) / 100 + " MB");
infoIs.close();
is.close();
zipIns.close();
} catch (Exception e) {
System.out.println("ipa包解析异常!");
}
return map;
}
public static File openStream(String url) {
String fileName = url.substring(url.lastIndexOf("."), url.length());
File file = null;
URL urlfile;
InputStream inStream = null;
OutputStream os = null;
try {
file = File.createTempFile("net_url", fileName);
urlfile = new URL(url);
inStream = urlfile.openStream();
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os) {
os.close();
}
if (null != inStream) {
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
}
调用方法
@GetMapping("/apk-parser")
public R apkParser(String url) {
String fileName = url.substring(url.lastIndexOf("."),url.length());
String prefix = fileName.substring(fileName.lastIndexOf("."));
File file = ApkAndIpaUtil.openStream(url);
Map<String, Object> map = null;
if (prefix.equals(".apk")){
map = ApkAndIpaUtil.parseApk(file);
}else if (prefix.equals(".ipa")){
map = ApkAndIpaUtil.parseIpa(file);
}else{
return R.error("请上传.apk /.ipa文件");
}
return R.ok(map);
}