应用场景
我需要批量提取出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