批量提取出apk中所需的文件

应用场景

我需要批量提取出apk中的classes.dex文件,如何在不解压的情况下快速提取出dex文件?在这里使用python自带的zipfile类,可以轻松的解决这个问题。

代码实现

#!/usr/bin/env python
# coding=utf-8


import os
import zipfile


path="D:/tao/test/apkstore" # this is apk files' store path
dex_path="D:/tao/test/dex/" # a directory  store dex files


apklist = os.listdir(path) # get all the names of apps

if not os.path.exists(dex_path):
    os.makedirs(dex_path)

for APK in apklist:
    portion = os.path.splitext(APK)

    if portion[1] == ".apk":
        newname = portion[0] + ".zip" # change them into zip file to extract dex files
        #print newname
        os.chdir(path)
        os.rename(APK,newname)

        apkname = portion[0]

        #zip_apk_path = os.path.join(path,APK) # get the zip files
        zip_apk_path = path+"/"+newname

        z = zipfile.ZipFile(zip_apk_path, 'r') # read zip files

        for filename in z.namelist():
            #print filename
            if filename.endswith(".dex"):
                dexfilename = apkname + ".dex"
                dexfilepath = os.path.join(dex_path, dexfilename)
                f = open(dexfilepath, 'w+') # eq: cp classes.dex dexfilepath
                f.write(z.read(filename))

print "all work done!"

借鉴了一下两篇文章的解决思路,达到了我想要的效果。
http://www.2cto.com/kf/201501/366441.html
http://bbs.csdn.net/topics/390902720

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值