Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Monkey patch to fix ValueError('ZIP does not support timestamps before 1980')
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