nodejs如何获取apk文件的信息(zip包)

var util = require('util'),
    fs = require('fs'),
    https = require('https'),
    spawn = require('child_process').spawn,

var exec = require('child_process').exec;
var aaptPath = "/etc/android-sdk/platform-tools/aapt";// aapt命令 路径


function getPackageMessage(path,apkName)
{
    var apkSize,
        developer;
    fs.stat(path+"/"+apkName+".apk", function (err, data) {
        if (err) throw err;
        apkSize=data.size;
        console.log(apkName+"文件大小:",apkSize);
    });
    var tmpStr;
    exec(aaptPath+" dump badging "+path+"/"+apkName+".apk",
        function (error, stdout, stderr) {
            console.log('stderr: ' + stderr);
            if (error !== null) {
                console.log('exec error: ' + error);
            }
            var str=stdout.toString();
//            console.log(str);
//        console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>",Buffer.isBuffer(stdout));
            var packageNameAnalysis1=String(str.match("name='.*' versionCode"));
            var packageNameAnalysis2=String(packageNameAnalysis1.match("'.*'"));
            var packageName=packageNameAnalysis2.substring (1,packageNameAnalysis2.indexOf('\'',1));//取包名
//            console.log("packageName",packageName);
            var versionCodeAnalysis1=String(str.match("versionCode='.*' versionName"));
            var versionCodeAnalysis2=String(versionCodeAnalysis1.match("'.*'"));
            var versionCode=versionCodeAnalysis2.substring (1,versionCodeAnalysis2.indexOf('\'',1));//取系统版本
//            console.log("versionCode",versionCode);
            var versionNameAnalysis1=String(str.match("versionName='.*'"));
            var versionNameAnalysis2=String(versionNameAnalysis1.match("'.*'"));
            var versionName=versionNameAnalysis2.substring (1,versionNameAnalysis2.indexOf('\'',1));//取版本号
//            console.log("versionName",versionName);
            var sdkVersionAnalysis1=String(str.match("sdkVersion:'.*'"));
            var sdkVersionAnalysis2=String(sdkVersionAnalysis1.match("'.*'"));
            var sdkVersion=sdkVersionAnalysis2.substring (1,sdkVersionAnalysis2.indexOf('\'',1));//取SDK版本号
//        console.log(sdkVersion);
            var applicationLabelAnalysis1=String(str.match("application-label:'.*'"));
            var applicationLabelAnalysis2=String(applicationLabelAnalysis1.match("'.*'"));
            var applicationLabel=applicationLabelAnalysis2.substring (1,applicationLabelAnalysis2.indexOf('\'',1));//取应用名称
//            console.log("applicationLabel",applicationLabel);
//        var url="https://play.google.com/store/search?q=pname:"+packageName;
//        console.log(url);
            //取开发者名称
            var options = {
                host:'play.google.com',
                path:'/store/search?q=pname:'+packageName
            };
            var html = "";//http获取html字符串
            https.get(options, function (res) {
                res.on('data',function (data) {//加载数据,一般会执行多次
                    html += data;
                }).on('end', function () {
                        var htmlAn=html.indexOf("/store/apps/developer?id");
                        var htmlAn2=html.substring(htmlAn);
                        developer=htmlAn2.substring(htmlAn2.indexOf(">")+1,htmlAn2.indexOf("<"));
                        console.log(apkName +"the developer is :",developer);
                        tmpStr=applicationLabel+"|"+packageName+"|"+versionName+"|"+versionCode+"|"+sdkVersion+"|"+apkSize+"|"+developer+"|";
//                        console.log("tmpStr is :",tmpStr);
                        //取class的URL
                        getClassUrl(path,apkName,tmpStr);
                    })
            });
        });
    //读取apk包信息
//    var aapt=spawn ("/etc/android-sdk/platform-tools/aapt",["dump","badging",path+"/"+apkName+".apk"]);
//    aapt.stdout.on('data', function (data) {
//        var str=data.toString();
//        var packageNameAnalysis1=str.match("name='.*' versionCode").toString();
//        var packageNameAnalysis2=packageNameAnalysis1.match("'.*'").toString();
//        var packageName=packageNameAnalysis2.substring (1,packageNameAnalysis2.indexOf('\'',1));//取包名
        console.log(packageName);
//        var versionCodeAnalysis1=str.match("versionCode='.*' versionName").toString();
//        var versionCodeAnalysis2=versionCodeAnalysis1.match("'.*'").toString();
//        var versionCode=versionCodeAnalysis2.substring (1,versionCodeAnalysis2.indexOf('\'',1));//取系统版本
        console.log(versionCode);
//        var versionNameAnalysis1=str.match("versionName='.*'").toString();
//        var versionNameAnalysis2=versionNameAnalysis1.match("'.*'").toString();
//        var versionName=versionNameAnalysis2.substring (1,versionNameAnalysis2.indexOf('\'',1));//取版本号
        console.log(versionName);
//        var sdkVersionAnalysis1=str.match("sdkVersion:'.*'").toString();
//        var sdkVersionAnalysis2=sdkVersionAnalysis1.match("'.*'").toString();
//        var sdkVersion=sdkVersionAnalysis2.substring (1,sdkVersionAnalysis2.indexOf('\'',1));//取SDK版本号
        console.log(sdkVersion);
//        var applicationLabelAnalysis1=str.match("application-label:'.*'").toString();
//        var applicationLabelAnalysis2=applicationLabelAnalysis1.match("'.*'").toString();
//        var applicationLabel=applicationLabelAnalysis2.substring (1,applicationLabelAnalysis2.indexOf('\'',1));//取应用名称
        console.log(applicationLabel);
//        var url="https://play.google.com/store/search?q=pname:"+packageName;
        console.log(url);
//        //取开发者名称
//        var options = {
//            host:'play.google.com',
//            path:'/store/search?q=pname:'+packageName
//        };
//        var html = "";//http获取html字符串
//        https.get(options, function (res) {
//            res.on('data',function (data) {//加载数据,一般会执行多次
//                html += data;
//            }).on('end', function () {
//                    var htmlAn=html.indexOf("/store/apps/developer?id");
//                    var htmlAn2=html.substring(htmlAn);
//                    developer=htmlAn2.substring(htmlAn2.indexOf(">")+1,htmlAn2.indexOf("<"));
                    console.log(apkName +"the developer is :",developer);
//                    tmpStr=applicationLabel+"|"+packageName+"|"+versionName+"|"+versionCode+"|"+sdkVersion+"|"+apkSize+"|"+developer+"|";
//                    //取class的URL
//                    getClassUrl(path,apkName,tmpStr);
//                })
//        });
//    });
//    aapt.stderr.on('data', function (data) {
//        console.log('stderr: ' + data);//输出错误数据
//    });
//    aapt.on('exit', function (code) {
        console.log('child process exited with code ' + code);//退出命令
//    });
}
function getClassUrl(path,apkName,tmp){
    var classUrl=new Array,
        apkImageStr="";
    var i=0;//classUrl的个数
    //取class的URL
    fs.readFile(path+'/'+apkName+'/classes.dex', "utf-8", function(err, file) {
        if(err) {
            console.log(err);
            return;
        }
        var file1=file;
        var classUrlTmp;
        while(true)
        {
            var num=file1.indexOf('//',1);
            var match = file1.match("(http://|https://)[\\w\\d:./]+");
            if(match == null){
                break ;
            }
            var matchStr=match.toString();
            var matchNum= matchStr.indexOf(',',1);

            if(matchStr.substring(0,matchNum)!=classUrlTmp)
            {
                classUrl[i]=matchStr.substring(0,matchNum);
                apkImageStr +=tmp+classUrl[i]+"|80\n";
//                console.log(classUrl[i]);
                i++;
            }
            classUrlTmp=matchStr.substring(0,matchNum);
            file1=file1.substring(num);

        }
        console.log(apkImageStr);
        console.log(apkName);
        fs.open("./download/"+apkName+".txt","a+",0777,function(e,fd){
            if(e) throw e;
            fs.write(fd,apkImageStr+"\n",null,'utf8',function(err){
                if(err) throw err;
                fs.closeSync(fd);
            })
        });
        //TODO
        var rmdirSync = (function(){
    function iterator(url,dirs){
        var stat = fs.statSync(url);
        if(stat.isDirectory()){
            dirs.unshift(url);//收集目录
            inner(url,dirs);
        }else if(stat.isFile()){
            fs.unlinkSync(url);//直接删除文件
        }
    }
    function inner(path,dirs){
        var arr = fs.readdirSync(path);
        for(var i = 0, el ; el = arr[i++];){
            iterator(path+"/"+el,dirs);
        }
    }
    return function(dir,cb){
        cb = cb || function(){};
        var dirs = [];
        try{
            iterator(dir,dirs);
            for(var i = 0, el ; el = dirs[i++];){
                fs.rmdirSync(el);//一次性删除所有收集到的目录
            }
            cb()
        }catch(e){//如果文件或目录本来就不存在,fs.statSync会报错,不过我们还是当成没有异常发生
            e.code === "ENOENT" ? cb() : cb(e);
        }
    }
})();
        rmdir.rmdirSync(path+"/"+apkName,function(){
            console.log("删除临时目录"+path+"/"+apkName+"以及子目录成功")
        });
    });
}
function getAllMessage(path,apkName){
    fs.mkdir(path+"/"+apkName);
    fs.rename(path+"/"+apkName+".apk",path+"/"+apkName+".zip",function(err){
        if (err) throw err;
        console.log("OK");
        var unzip = spawn("/usr/bin/unzip", ["-o", path+"/"+apkName+".zip", "-d", path+"/"+apkName]);
        unzip.on('exit', function (code) {
            console.log("解压完成");
            fs.open("./download/"+apkName+".txt","w+",0777,function(e,fd){
                if(e) throw e;
                fs.closeSync(fd);
            });
            fs.rename(path+"/"+apkName+".zip",path+"/"+apkName+".apk",function(errs){
                if(errs) throw errs;
                getPackageMessage (path,apkName);
            });
        })
    });
}

//getAllMessage("./upload", "com.snda.youni_185809");
exports.getAllMessage=getAllMessage;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值