Skip to content
This repository was archived by the owner on May 23, 2024. It is now read-only.

Commit 5d57449

Browse files
improved zipfile validation
added catching of zf.BadZipfile error which can occur during file reading
1 parent 83f370d commit 5d57449

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

sentinel_api/sentinel_api.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,20 @@ def _is_valid(self, zipfile, minsize=1000000):
333333
if not os.path.getsize(zipfile) > minsize:
334334
print('The downloaded scene is too small: {}'.format(os.path.basename(zipfile)))
335335
return False
336-
archive = zf.ZipFile(zipfile, 'r')
337336
try:
338-
corrupt = archive.testzip()
339-
except zlib.error:
340-
corrupt = zipfile
341-
archive.close()
337+
archive = zf.ZipFile(zipfile, 'r')
338+
try:
339+
corrupt = True if archive.testzip() else False
340+
except zlib.error:
341+
corrupt = True
342+
archive.close()
343+
except zf.BadZipfile:
344+
corrupt = True
342345
if corrupt:
343346
print('The downloaded scene is corrupt: {}'.format(os.path.basename(zipfile)))
344-
return False
345347
else:
346348
print('file seems to be valid.')
347-
return True
349+
return not corrupt
348350

349351
def _format_url(self, startindex, wkt_geometry, platform, date_filtering, **keywords):
350352
"""Format the search URL based on the arguments

0 commit comments

Comments
 (0)