Skip to content

Instantly share code, notes, and snippets.

@DastanIqbal
Last active June 4, 2018 07:32
Show Gist options
  • Save DastanIqbal/2c29194baa0e8507b30588ef7c104c0e to your computer and use it in GitHub Desktop.
Save DastanIqbal/2c29194baa0e8507b30588ef7c104c0e to your computer and use it in GitHub Desktop.
Extract apk, Operation , and compressed again
import zipfile
import sys
import os
import shutil
def extract(filename,extractpath="./extract/"):
if(os.path.isdir(extractpath)):
shutil.rmtree(extractpath)
zip_ref = zipfile.ZipFile(filename, 'r')
zip_ref.extractall(extractpath)
zip_ref.close()
def deleteFiles(extractpath="./extract/"):
if(os.path.isfile(extractpath+"/assets/file.txt")):
os.unlink(extractpath+"/assets/file.txt")
if(os.path.isdir(extractpath+"/folder")):
shutil.rmtree(extractpath+"/folder")
def compressFolder(filename,extractpath="./extract/"):
zipf = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED)
# The root directory within the ZIP file.
rootdir = os.path.basename(extractpath)
for root, dirs, files in os.walk(extractpath):
for file in files:
# Write the file named filename to the archive,
# giving it the archive name 'arcname'.
filepath = os.path.join(root, file)
parentpath = os.path.relpath(filepath, extractpath)
arcname = os.path.join(rootdir, parentpath)
zipf.write(filepath, arcname)
zipf.close()
try:
extract(sys.argv[1])
deleteFiles()
compressFolder(sys.argv[1]+"new")
except IndexError:
print("please pass zip file")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment