This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApkReader.cs
62 lines (53 loc) · 1.89 KB
/
ApkReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using AndroidXml;
using System.Data;
using System.IO;
using System.Linq;
using System.Xml;
using SharpCompress.Archives;
namespace ApkInfo
{
public static class PackageReader
{
/// <summary>
/// 获取Apk包名
/// </summary>
/// <param name="stream">文件流</param>
/// <returns></returns>
public static string GetPackageNameFromApk(Stream stream)
{
using (IArchive zipReader = ArchiveFactory.Open(stream))
{
IArchiveEntry adf = zipReader.Entries.FirstOrDefault(w => w.Key == "AndroidManifest.xml");
if (adf == null)
{
return string.Empty;
}
using (MemoryStream memoryStream = new MemoryStream())
{
adf.OpenEntryStream().CopyTo(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
AndroidXmlReader reader = new AndroidXmlReader(memoryStream);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
if (reader.Name != "package")//只读取包名的过滤条件
{
continue;
}
return reader.Value;
}
reader.MoveToElement();
break;
}
}
}
return string.Empty;
}
}
}
}