Skip to content

Commit e35a663

Browse files
committed
enhance download_and_extract
Signed-off-by: jerome_Hsieh <[email protected]>
1 parent 13b96ae commit e35a663

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

monai/apps/utils.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,18 @@ def download_and_extract(
327327
be False.
328328
progress: whether to display progress bar.
329329
"""
330-
with tempfile.TemporaryDirectory() as tmp_dir:
331-
filename = filepath or Path(tmp_dir, _basename(url)).resolve()
332-
download_url(url=url, filepath=filename, hash_val=hash_val, hash_type=hash_type, progress=progress)
333-
extractall(filepath=filename, output_dir=output_dir, file_type=file_type, has_base=has_base)
330+
urlFilenameExtension = ''.join(Path(".", _basename(url)).resolve().suffixes)
331+
if filepath:
332+
FilepathExtenstion = ''.join(Path(".", _basename(filepath)).resolve().suffixes)
333+
if urlFilenameExtension != FilepathExtenstion:
334+
raise NotImplementedError(
335+
f'The file types do not match: url={urlFilenameExtension}, but filepath={FilepathExtenstion}'
336+
)
337+
else:
338+
with tempfile.TemporaryDirectory() as tmp_dir:
339+
if filepath:
340+
filename = filepath
341+
else:
342+
filename = Path(tmp_dir, _basename(url)).resolve()
343+
download_url(url=url, filepath=filename, hash_val=hash_val, hash_type=hash_type, progress=progress)
344+
extractall(filepath=filename, output_dir=output_dir, file_type=file_type, has_base=has_base)

0 commit comments

Comments
 (0)