Created
December 8, 2018 08:26
-
-
Save manajay/3813f43bce2dffc5b79fdb7f1b230d54 to your computer and use it in GitHub Desktop.
解压 ipa 包为 App 文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: UTF-8 -*- | |
#!/usr/bin/python | |
import sys, getopt | |
# gz: 即gzip。通常仅仅能压缩一个文件。与tar结合起来就能够实现先打包,再压缩。 | |
# | |
# tar: linux系统下的打包工具。仅仅打包。不压缩 | |
# | |
# tgz:即tar.gz。先用tar打包,然后再用gz压缩得到的文件 | |
# | |
# zip: 不同于gzip。尽管使用相似的算法,能够打包压缩多个文件。只是分别压缩文件。压缩率低于tar。 | |
# | |
# rar:打包压缩文件。最初用于DOS,基于window操作系统。 | |
import gzip | |
import os | |
import tarfile | |
import zipfile | |
from cp_ipa import * | |
def main(argv): | |
inputfile = '' | |
outputfile = '' | |
try: | |
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) | |
except getopt.GetoptError: | |
#'输入提示语句: auto_test.py -h 也就是说短命令是 -h 短横线加 h' | |
print 'auto_test.py -i <inputfile> -o <outputfile>' | |
sys.exit(2) | |
for opt, arg in opts: | |
if opt == '-h': | |
print 'auto_test.py -i <inputfile> -o <outputfile>' | |
sys.exit() | |
elif opt in ("-i", "--ifile"): | |
inputfile = arg | |
print '输入的文件为:', inputfile | |
elif opt in ("-o", "--ofile"): | |
outputfile = arg | |
print '输出的文件为:', outputfile | |
fileName = os.path.basename(inputfile) | |
fileName = os.path.splitext(fileName)[0] | |
print '文件名称', fileName | |
unzipFile = './' + fileName | |
print '解压后的文件夹', unzipFile | |
un_zip(inputfile,unzipFile) | |
removedfile = unzipFile + "/Payload/" + fileName + ".app" | |
print '被移动的文件:', removedfile | |
#outputfile = './' + fileName + ".app" | |
print '最后的输出路径是: ',outputfile | |
shutil.move(removedfile,outputfile) | |
pass | |
# gz | |
# 因为gz一般仅仅压缩一个文件,全部常与其它打包工具一起工作。比方能够先用tar打包为XXX.tar,然后在压缩为XXX.tar.gz | |
# 解压gz,事实上就是读出当中的单一文件 | |
def un_gz(file_name): | |
"""ungz zip file""" | |
f_name = file_name.replace(".gz", "") | |
#获取文件的名称,去掉 | |
g_file = gzip.GzipFile(file_name) | |
#创建gzip对象 | |
open(f_name, "w+").write(g_file.read()) | |
#gzip对象用read()打开后,写入open()建立的文件里。 | |
g_file.close() | |
#关闭gzip对象 | |
# tar | |
# XXX.tar.gz解压后得到XXX.tar,还要进一步解压出来。 | |
# 注:tgz与tar.gz是同样的格式,老版本号DOS扩展名最多三个字符,故用tgz表示。 | |
# 因为这里有多个文件,我们先读取全部文件名称。然后解压。例如以下: | |
# 注:tgz文件与tar文件同样的解压方法。 | |
def un_tar(file_name): | |
# untar zip file""" | |
tar = tarfile.open(file_name) | |
names = tar.getnames() | |
if os.path.isdir(file_name + "_files"): | |
pass | |
else: | |
os.mkdir(file_name + "_files") | |
#因为解压后是很多文件,预先建立同名目录 | |
for name in names: | |
tar.extract(name, file_name + "_files/") | |
tar.close() | |
# zip | |
# 与tar类似,先读取多个文件名称,然后解压。例如以下: | |
def un_zip(file_name,destination): | |
"""unzip zip file""" | |
zip_file = zipfile.ZipFile(file_name) | |
if os.path.isdir(file_name + "_files"): | |
pass | |
else: | |
os.mkdir(destination) | |
for names in zip_file.namelist(): | |
zip_file.extract(names,destination) | |
zip_file.close() | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment