需要引用库:ICSharpCode.SharpZipLib.dll
图标路径:
//texture path string first = "res/mipmap-xxxhdpi-v4/app_icon.png"; string second = "res/drawable-xxxhdpi-v4/app_icon.png"; string third = "res/drawable-hdpi-v4/app_icon.png"; string fourth = "res/drawable-mdpi-v4/app_icon.png"; //string fifth = "res/drawable-ldpi-v4/app_icon.png"; string inputPath = "E:\\apk\\Camera.apk"; string outputPath = "E:\\apk\\UnZip\\";
图片的输出路径与输入路径相同
public void GetIconFromAPK(string inputpath, string outputpath) { if (!File.Exists(inputpath)) { Debug.LogError("Inputpath doesn't exist"); } else { if (!Directory.Exists(outputpath)) { Directory.CreateDirectory(outputpath); } string fileName = Path.GetFileName(inputpath).Replace(".apk", ".png"); ZipFile file = new ZipFile(inputpath);
//判断压缩文件中是否存在该文件 ZipEntry entry; entry = file.GetEntry(first); if (entry == null) { entry = file.GetEntry(second); } if (entry == null) { entry = file.GetEntry(third); } if (entry == null) { entry = file.GetEntry(fourth); Debug.LogError("Didn't find icon"); }
if (entry != null) { using (var s = file.GetInputStream(entry)) { using (FileStream streamWriter = File.Create(outputpath + fileName)) { byte[] data = new byte[2048]; int size = 0; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } } } } }
仅限apk中图片名为app_icon.png,manifest中可以读取到icon的位置,尚未研究