Skip to content

Instantly share code, notes, and snippets.

@booknara
Last active July 12, 2016 05:01
Show Gist options
  • Save booknara/6289643 to your computer and use it in GitHub Desktop.
Save booknara/6289643 to your computer and use it in GitHub Desktop.
This script help you extract image files from an APK file. Basic Usage (1) img_extractor.py --ifile <APK file> --ofile <Destination Directory> (2) img_extractor.py --ifile <APK file> --ofile . (3) img_extractor.py --ifile <APK file> --ofile ..
#!/usr/bin/python
# This script help you extract image files from an APK file.
# Author : Daehee Han (https://github.com/booknara, @daniel_booknara)
# Date : August 21 2013
# Basic Usage
# (1) img_extractor.py --ifile <APK file> --ofile <Destination Directory>
# (2) img_extractor.py --ifile <APK file> --ofile .
# (3) img_extractor.py --ifile <APK file> --ofile ..
import sys, getopt, os, zipfile, shutil
def main(argv):
currenDir = os.getcwd()
parentDir = os.path.normpath(os.path.join(currenDir, ".."))
try:
opts, args = getopt.getopt(argv, "hi:o", ["ifile=", "ofile="])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
inputfile = ''
outputDir = ''
wantedFileExtension = ['.png']
for opt, arg in opts:
if opt == "-h":
usage()
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
if arg == ".":
print "."
outputDir = currenDir
elif arg == "..":
print ".."
outputDir = parentDir
else:
outputDir = arg
else:
assert False, "unhandled option"
(inputfileName, inputfileExtension) = os.path.splitext(inputfile)
outputDir = outputDir + '/' + inputfileName
print
print "Input file is ", inputfile
print "Output file is ", outputDir
print
if not os.path.exists(outputDir):
pass
else:
shutil.rmtree(outputDir)
os.makedirs(outputDir)
print "Making Output directory..."
# Unzip APK file
print inputfile
z = zipfile.ZipFile(inputfile, "r")
fileCount = 0
for name in z.namelist():
(dirName, fileName) = os.path.split(name)
(dummyName, fileExtension) = os.path.splitext(name)
# only wanted files
if not fileExtension in wantedFileExtension:
continue
print fileName
newDir = outputDir + '/' + dirName
if not os.path.exists(newDir):
os.makedirs(newDir)
print "Making directory..."
if fileName == '':
# directory
pass
else:
# file
newFile = newDir + '/' + fileName
fd = open(newFile, "w")
fd.write(z.read(name))
fd.close()
fileCount = fileCount + 1
z.close()
print
print "%d files are extracted successfully" % fileCount
def usage():
print 'Usage'
print '(1) img_extractor.py --ifile <APK file> --ofile <Destination Directory>'
print '(2) img_extractor.py --ifile <APK file> --ofile .'
print '(3) img_extractor.py --ifile <APK file> --ofile ..'
print 'Output <Destination Directory>/<APK file named folder>'
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