@@ -26,7 +26,7 @@ def _create_if_not_exists(path: str, remove=True) -> None:
26
26
27
27
os .makedirs (path , exist_ok = True )
28
28
29
- def download (url : str , filename : str , unzip = False , unzip_path : str = None , force_download = False , clean = False ) -> None :
29
+ def download (url : str , filename : str , unzip = False , unzip_path : str = None , force_download = False , force_unzip = False , clean = False ) -> None :
30
30
"""
31
31
Download a file from a OneDrive url.
32
32
@@ -36,6 +36,7 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
36
36
:param unzip: Whether to unzip the file.
37
37
:param unzip_path: The path to unzip the file to. Default is current path.
38
38
:param force_download: Whether to force download the file even if it already exists.
39
+ :param force_unzip: Whether to force unzip and overwrite the file even if it already exists.
39
40
:param clean: Whether to clean the unzipped files after unzipping.
40
41
"""
41
42
@@ -49,9 +50,6 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
49
50
progress_bar = tqdm (total = total_size_in_bytes , unit = 'iB' , unit_scale = True ) if total_size_in_bytes > 1024 else None
50
51
block_size = 1024
51
52
52
- # parent if filename has a path else current path
53
- parent_path = os .path .split (filename )[0 ] if os .path .split (filename )[0 ] != '' else os .getcwd ()
54
-
55
53
if not os .path .exists (filename ) or force_download :
56
54
_create_if_not_exists (filename )
57
55
@@ -72,13 +70,18 @@ def download(url: str, filename: str, unzip=False, unzip_path: str = None, force
72
70
if unzip :
73
71
assert filename .endswith (".zip" ), "ERROR: file is not a zip file!"
74
72
75
- clean_unzip_path = unzip_path is not None and os .path .realpath (unzip_path ) not in os .path .realpath (filename )
73
+ unzip_path = unzip_path if unzip_path is not None else os .path .split (filename )[0 ]
74
+ clean_unzip_path = force_unzip and os .path .realpath (unzip_path ) not in os .path .realpath (filename )
76
75
77
- print ("Extracting files..." )
78
-
79
76
_create_if_not_exists (unzip_path , remove = clean_unzip_path )
77
+
78
+ if force_unzip :
79
+ print ("Warning: overwriting existing files!" )
80
+
80
81
with zipfile .ZipFile (filename , 'r' ) as zip_ref :
81
- zip_ref .extractall (unzip_path )
82
+ for file in tqdm (iterable = zip_ref .namelist (), total = len (zip_ref .namelist ()), desc = "Extracting files" ):
83
+ if not os .path .exists (os .path .join (unzip_path , file )) or force_unzip :
84
+ zip_ref .extract (member = file , path = unzip_path )
82
85
83
86
if clean :
84
87
os .remove (filename )
0 commit comments