Last active
December 5, 2016 02:08
-
-
Save tilacog/d23a09f8e9bd36d00b7f8c9538acba51 to your computer and use it in GitHub Desktop.
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
import zipfile | |
from io import BytesIO | |
def zipwalk(current_file, curr_path=''): | |
"""Recursively walks into a zipped file tree while yielding non-zipped | |
files content. | |
""" | |
data = BytesIO(current_file.read()) | |
try: | |
sub_zip = zipfile.ZipFile(data) | |
# base case | |
except zipfile.BadZipfile: | |
yield curr_path, data.getvalue() | |
# recurse case | |
else: | |
for inner_file in sub_zip.filelist: | |
new_path = curr_path + '/' + inner_file.filename.lstrip('/') | |
for i in zipwalk(sub_zip.open(inner_file), new_path): | |
yield i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment