private static void ReadApplicationIcon(ApkPackInfo info, String line) { if (String.IsNullOrEmpty(line)) { return; } string[] arr = line.Split(new[] { '\'' }, StringSplitOptions.RemoveEmptyEntries); info.ApplicationIcon = arr[arr.Length - 1]; }
private static void ReadPackage(ApkPackInfo info, String line) { if (String.IsNullOrEmpty(line)) { return; } string[] arr = line.Split(new[] { ' ', '=', ':', '\'' }, StringSplitOptions.RemoveEmptyEntries); info.PackageName = arr[2]; info.VersionCode = arr[4]; info.VersionName = arr[6]; }
private static void AddIcons(ApkPackInfo info, String line) { //application-icon-160:'res/drawable/icon.png' if (String.IsNullOrEmpty(line)) { return; } string[] arr = line.Split(new[] { ':', '\'' }, StringSplitOptions.RemoveEmptyEntries); if (arr.Length != 2) { return; } info.ApplicationIcons.Add(arr[0], arr[1]); }
/// <summary> /// 获取APK信息 /// </summary> /// <param name="apkPath">APK的绝对路径</param> /// <returns></returns> public ApkPackInfo GetApkInfo(string apkPath) { ApkPackInfo info; using (var p = new Process()) { p.StartInfo = new ProcessStartInfo(System.AppDomain.CurrentDomain.BaseDirectory + "aapt.exe", String.Format(" d badging \"{0}\"", apkPath)) { UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8 }; p.Start(); StreamReader output = p.StandardOutput; string line = output.ReadLine(); if (String.IsNullOrEmpty(line) || !line.StartsWith("package")) { throw new ArgumentException("参数不正确,无法正常解析APK包。输出结果为:" + line + Environment.NewLine + output.ReadToEnd()); } info = new ApkPackInfo(); do { if (String.IsNullOrEmpty(line)) { continue; } foreach (var action in ActionMap) { if (line.StartsWith(action.Key, StringComparison.CurrentCultureIgnoreCase)) { if (line.Contains("application-label-") && !line.Contains("application-label-zh_CN")) //不获取除中文名字之外的名字 { continue; } else { action.Value(info, line); } } } } while (!String.IsNullOrEmpty(line = output.ReadLine())); } return(info); }
private static void AddFeature(ApkPackInfo info, String line) { //uses-implied-feature:'android.hardware.screen.portrait','one or more activities have specified a portrait orientation' if (String.IsNullOrEmpty(line)) { return; } string[] arr = line.Split(new[] { ':', '\'', ',' }, StringSplitOptions.RemoveEmptyEntries); if (arr.Length != 3) { return; } var f = new ApkImpliedFeature(arr[1], arr[2]); info.ImpliedFeatures.Add(f); }
/// <summary> /// 解析APK包 /// </summary> /// <param name="apk_path">APK的绝对路径</param> /// <param name="md5">APK的MD5值,用作icon的名字</param> /// <returns></returns> private ApkPackInfo GetAPKInfo(string apk_path, string md5) { ApkPackInfo model = new ApkPackInfo(); try { string aaptFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "aapt.exe"); string icon_path = IOHelper.GetMapPath("/uploads/apk/"); if (!Directory.Exists(icon_path)) { Directory.CreateDirectory(icon_path); } string default_icon_name = md5 + ".image"; model = new ReadApkPackInfo(aaptFilePath).GetApkInfo(apk_path); model.MD5 = md5; //获取ICON IOHelper.UnZipPath(apk_path, icon_path, model.ApplicationIcon, default_icon_name); } catch (Exception) { IOHelper.DeleteFile(apk_path); //删除APK } return(model); }
/// <summary> /// 上传APK /// </summary> /// <returns></returns> public Hashtable Upload_APK(HttpPostedFileBase fileData) { Hashtable hash = new Hashtable(); if (fileData == null) { hash["msg"] = 0; hash["msgbox"] = "请上传文件"; return(hash); } string ServerPath = "/uploads/apk/"; try { string OldfileName = fileData.FileName; int file_size = fileData.ContentLength; //获取文件大小 KB string fileExt = IOHelper.GetFileExt(fileData.FileName); //文件扩展名,不含“.” if (fileExt.ToLower() != "apk") { hash["msg"] = 0; hash["msgbox"] = "只允许上传APK格式的文件"; return(hash); } string filePath = IOHelper.GetMapPath(ServerPath); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string saveName = Guid.NewGuid().ToString() + "." + fileExt; // 保存文件名称 fileData.SaveAs(filePath + saveName); //获取APK的MD5值 string md5 = IOHelper.GetMD5HashFromFile(filePath + saveName); //校验文件是否存在 if (System.IO.File.Exists(filePath + md5 + "." + fileExt)) { System.IO.File.Delete(filePath + saveName); //把刚刚上传的给删掉,只用原有的文件 } else //不存在,改名为md5值保存 { System.IO.File.Move(filePath + saveName, filePath + md5 + "." + fileExt); //给文件改名 } //解析APK包 ApkPackInfo ApkInfoModel = GetAPKInfo(filePath + md5 + ".apk", md5); if (ApkInfoModel == null) { hash["msg"] = 0; hash["msgbox"] = "解析APK文件出错"; return(hash); } else { hash["msg"] = 1; hash["msgbox"] = "APK处理成功"; hash["size"] = file_size; hash["md5"] = ApkInfoModel.MD5; hash["version"] = ApkInfoModel.VersionName; hash["version_code"] = ApkInfoModel.VersionCode; hash["down_url"] = ServerPath + md5 + ".apk"; hash["logo_img"] = ServerPath + md5 + ".image"; return(hash); } } catch (Exception ex) { hash["msg"] = 0; hash["msgbox"] = ex.Message; return(hash); } }