Skip to content

Commit 8ce0696

Browse files
committed
Add tar support
1 parent 42a019d commit 8ce0696

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
First stable release: `1.0.0`
88

9-
Current: `1.0.6`
9+
Current: `1.1.0`
1010

1111
*Do not require authentication.*
1212

onedrivedownloader/main.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import requests
22
from tqdm import tqdm
33
import zipfile
4+
import tarfile
45
import os
56
import shutil
67

@@ -26,7 +27,7 @@ def _create_if_not_exists(path: str, remove=True) -> None:
2627

2728
os.makedirs(path, exist_ok=True)
2829

29-
def download(url: str, filename: str, unzip=False, unzip_path: str = None, force_download=False, force_unzip=False, clean=False) -> str:
30+
def download(url: str, filename: str, unzip=True, unzip_path: str = None, force_download=False, force_unzip=False, clean=False) -> str:
3031
"""
3132
Download a file from a OneDrive url.
3233
@@ -84,7 +85,7 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
8485

8586
# unzip file if necessary
8687
if unzip:
87-
if filename.endswith(".zip"):
88+
if filename.endswith(".zip") or filename.endswith(".tar.gz"):
8889
unzip_path = unzip_path if unzip_path is not None else os.path.split(filename)[0]
8990
clean_unzip_path = force_unzip and os.path.realpath(unzip_path) not in os.path.realpath(filename)
9091
ret_path = unzip_path
@@ -94,10 +95,16 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
9495
if force_unzip:
9596
print("Warning: overwriting existing files!")
9697

97-
with zipfile.ZipFile(filename, 'r') as zip_ref:
98-
for file in tqdm(iterable=zip_ref.namelist(), total=len(zip_ref.namelist()), desc="Extracting files"):
99-
if not os.path.exists(os.path.join(unzip_path, file)) or force_unzip:
100-
zip_ref.extract(member=file, path=unzip_path)
98+
if filename.endswith(".zip"):
99+
with zipfile.ZipFile(filename, 'r') as zip_ref:
100+
for file in tqdm(iterable=zip_ref.namelist(), total=len(zip_ref.namelist()), desc="Extracting files"):
101+
if not os.path.exists(os.path.join(unzip_path, file)) or force_unzip:
102+
zip_ref.extract(member=file, path=unzip_path)
103+
elif filename.endswith(".tar.gz"):
104+
with tarfile.open(filename, 'r:gz') as tar_ref:
105+
for file in tqdm(iterable=tar_ref.getnames(), total=len(tar_ref.getnames()), desc="Extracting files"):
106+
if not os.path.exists(os.path.join(unzip_path, file)) or force_unzip:
107+
tar_ref.extract(member=file, path=unzip_path)
101108

102109
if clean:
103110
os.remove(filename)
@@ -109,7 +116,7 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
109116

110117

111118
if __name__ == "__main__":
112-
ln = "https://unimore365-my.sharepoint.com/:u:/g/personal/215580_unimore_it/EQ-DxzGOF7lBt90A601kvVEBR_ca9PtUdN_asesZ-F80bw?download=1"
119+
ln = 'https://unimore365-my.sharepoint.com/:u:/g/personal/215580_unimore_it/EdD9BE-36ohCpMy0_EKyYb0BnnSnrP7g8TOqTaeYsy-FCA?e=OJVriO'
113120
print('Downloading dataset')
114-
ret = download(ln, filename="./tmp/", unzip=True)
121+
ret = download(ln, filename="./tmp/", clean=True)
115122
print(ret)

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
setup(
77
name='onedrivedownloader',
88
packages=find_packages(),
9-
version='1.0.6',
9+
version='1.1.0',
1010
license='MIT',
1111
description='Python utility to download files through OneDrive',
1212
long_description=long_description,
1313
long_description_content_type="text/markdown",
1414
author='Lorenzo Bonicelli',
1515
author_email='[email protected]',
1616
url='https://github.com/loribonna/onedrivedownloader',
17-
download_url='https://github.com/loribonna/onedrivedownloader/archive/refs/tags/v1.0.6.zip',
17+
download_url='https://github.com/loribonna/onedrivedownloader/archive/refs/tags/v1.1.0.zip',
1818
keywords=['onedrive', 'downloader', 'download', 'python', 'utility'],
1919
install_requires=["requests","tqdm"],
2020
classifiers=[

0 commit comments

Comments
 (0)