Skip to content

Commit c0e11c2

Browse files
committed
fix windows path length error handling in unzip_zips
may change from error to warning in the future
1 parent c2480cb commit c0e11c2

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

src/neonutilities/unzip_and_stack.py

+33-28
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ def unzip_zipfile(zippath):
5555
5656
@author: Zachary Nickerson
5757
58-
Updated Tues Feb 15 2024 to use "with" to ensure zip files properly close out
58+
Updated Feb 2025 to use "with" so that zip files properly close out
5959
and fix error handling for Windows filepath character length limits
6060
61-
Folders should not be downloaded if the path length was too long, so this
62-
might be redundant (see api_helpers.py download_url function)
6361
"""
6462

6563
# Error handling on inputs
@@ -72,29 +70,34 @@ def unzip_zipfile(zippath):
7270

7371
if level == "all":
7472
with zipfile.ZipFile(zippath, 'r') as zip_ref:
75-
tl = zip_ref.namelist()
76-
73+
tl = zip_ref.namelist(); #print('tl:',tl)
7774
# Construct full paths as they will be after extraction
78-
full_extracted_paths = [os.path.join(
79-
zippath, zipname) for zipname in tl]
80-
81-
# Error handling for filepath character lengths
75+
# not sure what the full_extracted_path should be here
76+
# this is used in stack_by_table, eg.
77+
# litterlst = nu.stack_by_table("./testdata/NEON_litterfall.zip", savepath="envt")
78+
full_extracted_paths = [os.path.join(os.path.abspath(zippath).replace('.zip',''), zipname) for zipname in tl]
79+
# print('full_extracted_paths:',full_extracted_paths)
80+
# print('len(full_extracted_paths):',[len(x) for x in full_extracted_paths])
81+
82+
# Error handling for filepath character length limitations on Windows
83+
# Should this be a warning? Or check if the Windows system has LongPathsEnabled?
8284
if any(len(x) > 260 for x in full_extracted_paths) and platform.system() == "Windows":
8385
longest_path = max(full_extracted_paths, key=len)
84-
raise OSError(f"Longest filepath is {len(longest_path)} characters long. "
85-
"Filepaths on Windows are limited to 260 characters. "
86-
"Move files closer to the root directory or enable "
86+
raise OSError(f"Filepaths on Windows are limited to 260 characters. "
87+
"Trying to extract a filepath that is {len(longest_path)} characters long. "
88+
"Move your working directory closer to the root directory or enable "
8789
"long path support in Windows through the Registry Editor.")
90+
else:
91+
# Unzip file and get list of zips within
92+
zip_ref.extractall(path=outpath)
93+
zps = glob.glob(zippath[:-4] + "/*.zip")
8894

89-
# Unzip file and get list of zips within
90-
zip_ref.extractall(path=outpath)
95+
# If there are more zips within parent zip file, unzip those as well
96+
# does this happen anymore? this might be deprecated.
97+
# level as an input might also be deprecated
9198

92-
# If there are more zips within parent zip file, unzip those as well
93-
# does this happen anymore? this might be deprecated.
94-
# level as an input might also be deprecated
95-
zps = glob.glob(zippath[:-4] + "/*.zip")
96-
if len(zps) > 0:
97-
print('need an example to properly code this up.\n')
99+
if len(zps) > 0:
100+
print('Multiple zip files are contained within the parent zip file. Need an example to properly code this up.\n')
98101

99102
if level == "in":
100103
zps = glob.glob(outpath+"/*.zip")
@@ -105,18 +108,20 @@ def unzip_zipfile(zippath):
105108

106109
# Construct full paths as they will be after extraction
107110
full_extracted_paths = [os.path.join(
108-
zippath, zipname) for zipname in tl]
111+
zps[i].replace('.zip',''), zipname) for zipname in tl]
112+
# print('full extracted paths:',full_extracted_paths)
113+
# print('len(full_extracted_paths):',[len(x) for x in full_extracted_paths])
109114

110-
# Error handling for filepath character lengths
115+
# Error handling for filepath character length limitations on Windows
111116
if any(len(x) > 260 for x in full_extracted_paths) and platform.system() == "Windows":
112117
longest_path = max(full_extracted_paths, key=len)
113-
raise OSError(f"Longest filepath is {len(longest_path)} characters long. "
114-
"Filepaths on Windows are limited to 260 characters. "
115-
"Move files closer to the root directory or enable "
118+
raise OSError(f"Filepaths on Windows are limited to 260 characters. "
119+
"Trying to extract a filepath that is {len(longest_path)} characters long. "
120+
"Move your working directory closer to the root directory or enable "
116121
"long path support in Windows through the Registry Editor.")
117-
118-
outpathi = zps[i][:-4]
119-
zip_refi.extractall(path=outpathi)
122+
else:
123+
outpathi = zps[i][:-4]
124+
zip_refi.extractall(path=outpathi)
120125
os.remove(zps[i])
121126

122127
return None

0 commit comments

Comments
 (0)