-
-
Notifications
You must be signed in to change notification settings - Fork 32k
ZipFile.open() should not ignore compression, new file should default to today rather than 1980 #121402
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
Comments
…dd .newfile() to keep compression
Why do you use |
That would cause the (streamed) file to be archived with a timestamp of 1980, which is the first bug. |
As for using the archive default compression by default, this is more complex question. |
Exactly and that's what my suggested change does, see:
Please note that the second bug I'm trying to fix is not merely using a default compression (I don't want to change the default behavior) but what happens is that the programmer explicitly specifies the compression (line 8) which is then ignored when adding files (lines 20 and 26). |
Unfortunately, embedded timestamps are the most common problem for Reproducible Builds and changing this default might have unintended consequences. |
I'm a fan of the logic in writestr, would using that when a str filename is passed would satisfy this request I think. cpython/Lib/zipfile/__init__.py Lines 1978 to 1981 in 5ab66a8
|
Bug report
Bug description:
When streaming, writing to a zip file, it internally creates a ZipInfo object without explicitly setting date_time, causing a timestamp of 1980 to be used. I believe in almost all cases, using the current timestamp makes more sense, I rarely zip files from 1980. To work around that issue, I create the ZipInfo object myself and pass it to the open() method, which causes another bug where the specified compression is ignored and the file is simply stored.
After some minor investigation, I've found issue #113971 which at least hinted towards what I now use as workaround but otherwise didn't fix this:
So you'd create a new ZipFile specifying a
compression
but afteropen()
, you're just storing uncompressed:In other words, I expect that:
a) when using open("file"...), it should use the current time as mtime by default
b) when creating a ZipInfo object to specify another date or another setting for the new file, it should inherit the previously configured compression
Both assumptions are wrong, which is what I'm suggesting to fix.
Here's a reproducer and I'll add a PR as suggested fix...
https://gist.github.com/c0xc/b54c005b296cdf6378ce65dd4aff3fe7
CPython versions tested on:
3.11, CPython main branch
Operating systems tested on:
Linux
Linked PRs
The text was updated successfully, but these errors were encountered: