python / python/cpython

ZipFile.open() should not ignore compression, new file should default to today rather than 1980

Đang mở
#121,402 7 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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:

Today people pass ZipInfo instances into APIs that accept them such as ZipFile.writestr() in order to control how individual items are put into a zip archive as such ...
zip_info.compress_type

So you'd create a new ZipFile specifying a compression but after open(), you're just storing uncompressed:

zip_info = zipfile.ZipInfo("file")
# zip_info.compress_type, ._compresslevel are wrong
zip_file.open(zip_info, mode="w", force_zip64=True)

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
  • gh-121405

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với reproducer được liên kết và hành vi của ZipFile.open() và ZipInfo được mô tả trong báo cáo. Kiểm tra cách việc ghi một entry mới xác định timestamp mặc định và việc nén của nó khi một đối tượng ZipInfo được cung cấp; được xem là hoàn tất khi cả timestamp mong đợi và hành vi nén đã cấu hình đều được đề cập, mặc dù issue trỏ đến PR được liên kết gh-121405.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.