Skip to content

Instantly share code, notes, and snippets.

@vikramsoni2
Created July 10, 2021 02:08
Show Gist options
  • Save vikramsoni2/029647ec8cbb4a525383e494f3e07531 to your computer and use it in GitHub Desktop.
Save vikramsoni2/029647ec8cbb4a525383e494f3e07531 to your computer and use it in GitHub Desktop.
extract zip file containing github repo witout the top level directory
from requests import get
from io import BytesIO
from zipfile import ZipFile
import os
from pathlib import Path
destination = "testproject"
file_url = 'https://github.com/vikramsoni2/aihubcli/archive/refs/heads/main.zip'
request = get(file_url)
zf = ZipFile(BytesIO(request.content))
def createdirs(filename):
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
for n in [n for n in zf.namelist() if not n.endswith('/')]:
target_file = n.replace(zf.namelist()[0], destination+'/')
createdirs(target_file)
with open(target_file, "wb") as f:
f.write(zf.read(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment