zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }
    }
  • 相关阅读:
    PHP date函数时间相差8个小时解决办法
    PHP中include()与require()的区别说明
    post与get区别
    MFC 对话框控件自动布局
    Apache php Mysql部署(一)下载安装
    apache 重定向
    clistctrl失去焦点高亮显示选中行
    格式化字符串
    unicode编码与utf-8 区别
    string的实现
  • 原文地址:https://www.cnblogs.com/refuge/p/11347631.html
Copyright © 2011-2022 走看看