Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set strict_timestamps=False to ZipFile #990

Closed
wants to merge 1 commit into from

Conversation

kasgel
Copy link

@kasgel kasgel commented Jul 29, 2024

Related to #958

Currently if the system time is before 1980-01-01, save of new PPTX file fails due to following error:


Traceback (most recent call last):
  File "test.py", line 19, in main
    prs.save('test.pptx')
  File "/packages/pptx/presentation.py", line 39, in save
    self.part.save(file)
  File "/packages/pptx/parts/presentation.py", line 107, in save
    self.package.save(path_or_stream)
  File "/packages/pptx/opc/package.py", line 153, in save
    PackageWriter.write(pkg_file, self._rels, tuple(self.iter_parts()))
  File "/packages/pptx/opc/serialized.py", line 76, in write
    cls(pkg_file, pkg_rels, parts)._write()
  File "/packages/pptx/opc/serialized.py", line 81, in _write
    self._write_content_types_stream(phys_writer)
  File "/packages/pptx/opc/serialized.py", line 91, in _write_content_types_stream
    phys_writer.write(
  File "/packages/pptx/opc/serialized.py", line 220, in write
    self._zipf.writestr(pack_uri.membername, blob)
  File "/usr/local/lib/python3.9/zipfile.py", line 1783, in writestr
    zinfo = ZipInfo(filename=zinfo_or_arcname,
  File "/usr/local/lib/python3.9/zipfile.py", line 361, in __init__
    raise ValueError('ZIP does not support timestamps before 1980')
ValueError: ZIP does not support timestamps before 1980

There exists a flag in zipfile.ZipFile called strict_timestamps=False that allows creation of a new ZipFile even if system time is before 1980. In such case, ZipFile forces the timestamp to 1980-01-01:

The strict_timestamps argument, when set to False, allows to zip files older than 1980-01-01 at the cost of setting the timestamp to 1980-01-01. Similar behavior occurs with files newer than 2107-12-31, the timestamp is also set to the limit.
(source)

This PR adds the strict_timestamps=False parameter

scanny added a commit that referenced this pull request Aug 3, 2024
Accommodate system dates before 1980-01-01.

This should have no effect for users with "normal" system clocks but
allows the package to save files in the unusual case the system clock is
set to a date prior to 1980.
@scanny scanny closed this in #995 Aug 3, 2024
@scanny scanny closed this in 284fc01 Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant