c# 获取 Apk ,Aar 文件包名

最近项目有个需求,需要拿到前端上传的Apk或者Aar文件里面的包名.

在这里贡献出来,方便有需求的小伙伴.

项目是 asp.net core 2.2 

需要安装 nuget :

AndroidXml
SharpCompress
using AndroidXml;
using SharpCompress.Archives;
using System.Data;
using System.IO;
using System.Linq;
using System.Xml;

namespace ********
{
    /// <summary>
    /// 包阅读器
    /// </summary>
    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;
            }
        }



        /// <summary>
        /// 获取aar包名
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <returns></returns>
        public static string GetPackageNameFromAar(Stream stream)
        {
            using (IArchive zipReader = ArchiveFactory.Open(stream))
            {
                IArchiveEntry adf = zipReader.Entries.FirstOrDefault(w => w.Key == "AndroidManifest.xml");

                if (adf == null)
                {
                    return string.Empty;
                }


                DataSet ds = new DataSet();
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    adf.OpenEntryStream().CopyTo(memoryStream);
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    ds.ReadXml(memoryStream);
                }
                DataRowCollection rows = ds.Tables["manifest"].Rows;
                object result = rows[0]["package"];
                return result?.ToString() ?? string.Empty;
            }
        }
    }
}

 

c# 获取 Apk ,Aar 文件包名

上一篇:jetson nano opencv3.4.x安装


下一篇:WPF窗体自适应分辨率