Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import zipfile
- from io import BytesIO
- def zipwalk(current_file, file_handling_func, curr_path='/'):
- """Recursively walks into a zipped file.
- Arguments:
- ----------
- - current_file : file
- a file object opened in `read-bytes` mode
- - file_handling_func : function
- function to handle a non-zip file content.
- zipwalk will call it with the following arguments:
- - content: io.BytesIO
- - zip_file_object: zipfile.ZipExtFile (not a zipped file itself).
- - path : string
- - curr_path : string
- the current path inside outmost zip file.
- """
- content = BytesIO(current_file.read())
- if zipfile.is_zipfile(content):
- subzip = zipfile.ZipFile(content)
- # recurse
- return [
- zipwalk(
- subzip.open(i.filename),
- file_handling_func,
- curr_path + current_file.name + '/'
- )
- for i in subzip.filelist
- ]
- else:
- # handle non-zip file contents
- file_handling_func(content, current_file, curr_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement