Skip to content

Instantly share code, notes, and snippets.

@JAEarly
Created May 7, 2020 12:02
Show Gist options
  • Save JAEarly/ab2349af9b30ffbf479dce8b208e6abe to your computer and use it in GitHub Desktop.
Save JAEarly/ab2349af9b30ffbf479dce8b208e6abe to your computer and use it in GitHub Desktop.
Download and extract a zip from Dropbox
import os
import urllib.request
import zipfile
from tqdm import tqdm
from util import create_dir_if_not_exist
ZIP_URL = "https://www.dropbox.com/s/<drop_box_id>/<file_name>?dl=1"
ZIP_PATH = "<path_to_download_dest>"
PROCESSED_DIR = "<path_to_extract_dest>"
def download():
if not os.path.exists(ZIP_PATH):
print('Downloading from Dropbox')
response = urllib.request.urlopen(ZIP_URL)
total_length = int(response.info()['Content-Length'])/1000000
size = 16 * 1024
with open(ZIP_PATH, 'wb') as f:
with tqdm(total=total_length, unit='MB',
bar_format='{l_bar}{bar}| {n:.1f}/{total:.1f}MB [{elapsed}<{remaining}, ' '{rate_fmt}{postfix}]') \
as pbar:
while True:
chunk = response.read(size)
if not chunk:
break
f.write(chunk)
pbar.update(len(chunk)/1000000)
response.close()
else:
print('Zip file already found')
def extract():
print('Extracting from zip')
create_dir_if_not_exist(PROCESSED_DIR)
with zipfile.ZipFile(ZIP_PATH, 'r') as zip_ref:
for member in tqdm(zip_ref.infolist(), desc='Extracting'):
zip_ref.extract(member, PROCESSED_DIR)
if __name__ == "__main__":
download()
extract()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment