Skip to content

Instantly share code, notes, and snippets.

@JamesPHoughton
Created September 15, 2014 14:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JamesPHoughton/0f4f269e93a2b85958d8 to your computer and use it in GitHub Desktop.
Save JamesPHoughton/0f4f269e93a2b85958d8 to your computer and use it in GitHub Desktop.
Recursively unpack zip files in python
from zipfile import ZipFile
def unpack_zip(zipfile='', path_from_local=''):
filepath = path_from_local+zipfile
extract_path = filepath.strip('.zip')+'/'
parent_archive = ZipFile(filepath)
parent_archive.extractall(extract_path)
namelist = parent_archive.namelist()
parent_archive.close()
for name in namelist:
try:
if name[-4:] == '.zip':
unpack_zip(zipfile=name, path_from_local=extract_path)
except:
print 'failed on', name
pass
return extract_path
# you can just call this with filename set to the relative path and file.
path = unpack_zip(filename)
@JamesPHoughton
Copy link
Author

I had a zip file full of zip files, each of which might contain zip files, and I wanted a matching folder structure.

Wrote this for a mac.

@kenzo251
Copy link

i recommend you to not try this on 42.zip(your computer probably can't handle a few petabytes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment