Skip to content

Instantly share code, notes, and snippets.

@denysbutenko
Created September 4, 2014 16:26
Show Gist options
  • Save denysbutenko/cb69d89fd5682865093e to your computer and use it in GitHub Desktop.
Save denysbutenko/cb69d89fd5682865093e to your computer and use it in GitHub Desktop.
import os
from zipfile import ZipFile
from StringIO import StringIO
from urllib import urlopen
from sys import platform as _platform
def unzip_from_stream(url, dest_dir):
original_zip = ZipFile(StringIO(urlopen(url).read()))
if _platform == "win32":
original_zip.extractall(dest_dir)
elif _platform in ["linux", "linux2", "darwin"]:
new_zip = ZipFile(StringIO(), 'w')
with original_zip as f:
for item in f.infolist():
new_filename = item.filename.replace('\\', '/')
new_zip.writestr(new_filename, f.read(item))
new_zip.extractall(dest_dir)
new_zip.close()
original_zip.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment