How to extract file from zip without maintaining directory structure in Python? How to extract file from zip without maintaining directory structure in Python? python python

How to extract file from zip without maintaining directory structure in Python?


You can use zipfile.ZipFile.open:

import shutilimport zipfilewith zipfile.ZipFile('/path/to/my_file.apk') as z:    with z.open('/res/drawable/icon.png') as zf, open('temp/icon.png', 'wb') as f:        shutil.copyfileobj(zf, f)

Or use zipfile.ZipFile.read:

import zipfilewith zipfile.ZipFile('/path/to/my_file.apk') as z:    with open('temp/icon.png', 'wb') as f:        f.write(z.read('/res/drawable/icon.png'))