public apkreader(string path) { var manifest = "AndroidManifest.xml"; //读取apk,通过解压的方式读取 using (var zip = ZipFile.Read(path)) { using (Stream zipstream = zip[manifest].OpenReader()) { //将解压出来的文件保存到一个路径(必须这样) using (var fileStream = File.Create(manifest, (int)zipstream.Length)) { // Initialize the bytes array with the stream length and then fill it with data byte[] bytesInStream = new byte[zipstream.Length]; zipstream.Read(bytesInStream, 0, bytesInStream.Length); // Use write method to write to the file specified above fileStream.Write(bytesInStream, 0, bytesInStream.Length); } } } //读取解压文件的字节数 byte[] data = File.ReadAllBytes(manifest); if (data.Length == 0) { throw new IOException("Empty file"); } #region 读取文件内容 using (var stream = new MemoryStream(data)) { var reader = new AndroidXmlReader(stream); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: { AndroidInfo info = new AndroidInfo(); androidInfos.Add(info); info.Name = reader.Name; info.Settings = new List <AndroidSetting>(); for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); AndroidSetting setting = new AndroidSetting() { Name = reader.Name, Value = reader.Value }; info.Settings.Add(setting); } reader.MoveToElement(); break; } } } } #endregion File.Delete(manifest); StringBuilder builder = new StringBuilder(); foreach (var androidInfo in androidInfos) { builder.Append(string.Format("{0}:", androidInfo.Name)); foreach (var setting in androidInfo.Settings) { builder.Append("{"); builder.Append(string.Format("'{0}':'{1}'", setting.Name, setting.Value)); builder.Append("},"); } builder.Append("\n\n"); } Console.WriteLine(builder.ToString()); }
/// <summary> /// 获取主配置文件信息 /// </summary> /// <param name="apkStream">apk流</param> /// <returns>安卓信息列表</returns> public static IList <AndroidInfo> GetManifestInfo(Stream apkStream) { if (apkStream == null) { throw new ArgumentNullException("apk流不能为null"); } var androidInfos = new List <AndroidInfo>(); byte[] manifestData = null; using (var zipfile = new ICSharpCode.SharpZipLib.Zip.ZipFile(apkStream)) { foreach (ICSharpCode.SharpZipLib.Zip.ZipEntry item in zipfile) { if (item.Name.ToLower() == "androidmanifest.xml") { using (Stream strm = zipfile.GetInputStream(item)) { using (BinaryReader s = new BinaryReader(strm)) { manifestData = s.ReadBytes((int)item.Size); } } break; } } zipfile.Close(); } if (manifestData.IsNullOrLength0()) { return(null); } #region 读取文件内容 using (var stream = new MemoryStream(manifestData)) { using (var reader = new AndroidXmlReader(stream)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: AndroidInfo info = new AndroidInfo(); androidInfos.Add(info); info.Name = reader.Name; info.Settings = new List <KeyValueInfo <string, string> >(); for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); var setting = new KeyValueInfo <string, string>() { Key = reader.Name, Value = reader.Value }; info.Settings.Add(setting); } reader.MoveToElement(); break; } } reader.Close(); } } #endregion return(androidInfos); }