#更新zip文件中某一个文件
import os
import shutil
import tempfile
import zipfile
from rat_tool.pack import *
toolpath= os.getcwd()+"/tool"
smalipath=os.path.join( toolpath,"smali.jar") def updateZip(zipname, filename, data): tmpfd, tmpname = tempfile.mkstemp(dir=os.path.dirname(zipname)) os.close(tmpfd) with zipfile.ZipFile(zipname, 'r') as zin: with zipfile.ZipFile(tmpname, 'w') as zout: zout.comment = zin.comment for item in zin.infolist(): if item.filename != filename: zout.writestr(item, zin.read(item.filename)) os.remove(zipname) os.rename(tmpname, zipname) with zipfile.ZipFile(zipname, mode='a', compression=zipfile.ZIP_DEFLATED) as zf: zf.writestr(filename, data)
使用:
#----更新apk文件中的classes3.dex文件------------------------------------------------------------------
def main():
#将修改后的smali文件重新打包成dex,替换原来包中dex
indir=r"D:\pa\apk\haoyishen\smali_classes3"
outfile=r"D:\pa\apk\classes3.dex"
#smali(indir, outfile)
tempzip = r"D:\pa\apk\haoyishenbd.apk" buff=open(outfile,"rb").read()
#updateZip(tempzip, "classes3.dex", buff) signapk(tempzip, tempzip) print"" if __name__ == '__main__':
main()