@@ -55,11 +55,9 @@ def unzip_zipfile(zippath):
55
55
56
56
@author: Zachary Nickerson
57
57
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
59
59
and fix error handling for Windows filepath character length limits
60
60
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)
63
61
"""
64
62
65
63
# Error handling on inputs
@@ -72,29 +70,34 @@ def unzip_zipfile(zippath):
72
70
73
71
if level == "all" :
74
72
with zipfile .ZipFile (zippath , 'r' ) as zip_ref :
75
- tl = zip_ref .namelist ()
76
-
73
+ tl = zip_ref .namelist (); #print('tl:',tl)
77
74
# 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?
82
84
if any (len (x ) > 260 for x in full_extracted_paths ) and platform .system () == "Windows" :
83
85
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 "
87
89
"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" )
88
94
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
91
98
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 ' )
98
101
99
102
if level == "in" :
100
103
zps = glob .glob (outpath + "/*.zip" )
@@ -105,18 +108,20 @@ def unzip_zipfile(zippath):
105
108
106
109
# Construct full paths as they will be after extraction
107
110
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])
109
114
110
- # Error handling for filepath character lengths
115
+ # Error handling for filepath character length limitations on Windows
111
116
if any (len (x ) > 260 for x in full_extracted_paths ) and platform .system () == "Windows" :
112
117
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 "
116
121
"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 )
120
125
os .remove (zps [i ])
121
126
122
127
return None
0 commit comments