Created
June 24, 2020 23:16
-
-
Save kylemanna/b8ab2d8c85836358b80b571eab24c83c to your computer and use it in GitHub Desktop.
Monkey patch to fix ValueError('ZIP does not support timestamps before 1980')
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ZipInfoTimeless(zipfile.ZipInfo): | |
def __init__(self, *args, **kwargs): | |
# Patch year to 1980 if given a datetime before 1980. Also jump through hoops because tuples are | |
# immutable. | |
if 'date_time' in kwargs and kwargs['date_time'][0] < 1980: | |
dt = list(kwargs['date_time']) | |
dt[0] = 1980 | |
kwargs['date_time'] = tuple(dt) | |
elif len(args) > 1 and args[1][0] < 1980: | |
dt = list(args[1]) | |
dt[0] = 1980 | |
args_list = list(args) | |
args_list[1] = tuple(dt) | |
args = tuple(args_list) | |
super().__init__(*args, **kwargs) | |
# Monkey patch ZipInfo class to be less concerned about time. Python 3.8 adds ability to pass | |
# strict_timestamps=False to accomplish the same behavior. Otherwise a ValueError exception is raised. | |
zipfile.ZipInfo = ZipInfoTimeless |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment