Skip to content

Instantly share code, notes, and snippets.

@Atala
Created October 16, 2014 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Atala/05ac6f8487fc0e81c197 to your computer and use it in GitHub Desktop.
Save Atala/05ac6f8487fc0e81c197 to your computer and use it in GitHub Desktop.
Unzip a file in python - note that extractall() is sensible to traversal attacks
import zipfile,os.path
def unzip(source_filename, dest_dir):
with zipfile.ZipFile(source_filename) as zf:
for member in zf.infolist():
# Path traversal defense copied from
# http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
words = member.filename.split('/')
path = dest_dir
for word in words[:-1]:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir, ''): continue
path = os.path.join(path, word)
zf.extract(member, path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment