Skip to content

Instantly share code, notes, and snippets.

@tilacog
Last active December 5, 2016 02:08
Show Gist options
  • Save tilacog/d23a09f8e9bd36d00b7f8c9538acba51 to your computer and use it in GitHub Desktop.
Save tilacog/d23a09f8e9bd36d00b7f8c9538acba51 to your computer and use it in GitHub Desktop.
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