惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 我想我是青蛙

将golang程序注册为windows服务 CentOS上安装spark standalone mode(转载) - 我想我是青蛙 关于听歌这回事 - 我想我是青蛙 golang 读取mongob数据写入sqlserver golang 通用Contains方法 - 我想我是青蛙 golang读取文件信息插入mongodb PetaPoco介绍 - 我想我是青蛙 白话MongoDB(三)(转载) - 我想我是青蛙 白话MongoDB(二)(转载) 白话MongoDB(一) (转载) - 我想我是青蛙 给文章加入关键字链接 针对firefox ie6 ie7 ie8的css样式hack (转载) 好久不写日志了,现在开始,好好写了。。 sharepoint 查询calendar recurrence sharepoint之lookup字段 sharepoint获取Audiences 获取exchangeserve的calendar的item sharepoint错误处理 c#上传下载ftp(支持断点续传)
c#读取apk 信息
我想我是青蛙 · 2013-06-09 · via 博客园 - 我想我是青蛙

  昨天遇到一个问题,需要通过c#读取apk包的信息。baidu,google了一大堆东西,也没有找到相关的资料,有的也只是通过kvm把jar转换为.net程序集来调用,试了一下,各种不稳定,各种错误。

  在大概11点半的时候,终于在codeplex上面找到一个http://androidxmldotnet.codeplex.com/ 项目,可以解析AndroidManifest.xml。

     具体代码如下

using AndroidXml;
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace APKRead
{
    class NamespaceInfo
    {
        public string Prefix { get; set; }
        public string Uri { get; set; }
    }


    class Program
    {

        static List<AndroidInfo> androidInfos = new List<AndroidInfo>();


        static void Main(string[] args)
        {
            //要分析的文件名称
            var manifest = "AndroidManifest.xml";

            //读取apk,通过解压的方式读取
            using (var zip = ZipFile.Read("News.apk"))
            {
                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>
    /// android应用程序信息
    /// </summary>
    public class AndroidInfo
    {
        public string Name { get; set; }

        public List<AndroidSetting> Settings { get; set; }
    }

    /// <summary>
    /// 设置
    /// </summary>
    public class AndroidSetting
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
}

      

  引用的Ionic.Zip库可以直接通过 nuget下载。

  快12点的时候,终于好了,然后睡觉去了。