Enhance `zipfile` to support decoding comments using `metadata_encoding`
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Feature or enhancement
Proposal:
In the current implementation, ZipFile accepts a metadata_encoding parameter to decode member filenames automatically into strings. However, {ZipFile,ZipInfo}.comment remains as raw bytes. This creates an API inconsistency and leaves the burden of proper decoding to the user.
According to the ZIP specification, a member's comment must be decoded using UTF-8 if its EFS flag bit is set. Otherwise, it should fallback to cp437 (per spec) or the local encoding (in practice), which typically aligns with the encoding used for filenames.
Currently, to properly read comments, users must write redundant boilerplate to manually check the EFS flag and apply the appropriate codec. This severely defeats the convenience introduced by the metadata_encoding parameter.
Proposed Solution
Prerequisite
For ZipInfo objects to handle comment encoding/decoding properly, there is a need to access the specified encoding context from the parent archive.
- Approach A: Pass down the context as an internal attribute, such as
_metadata_encoding. This aligns with the strategy used in PR gh-152846, which preserves the EFS flag during archive modification. - Approach B: Bind the parent
ZipFileobject via an internal attribute like_zipfile. This dynamically links the active encoding and enables future structural guardrails, such as detecting and warning against unsafe cross-archive metadata mutations, though care must be taken regarding pickling behavior.
Implementation
While updating {ZipFile,ZipInfo}.comment to be string-based would be the cleanest API, it is likely undesired due to breaking backward compatibility.
Alternatively, we can introduce a comment_text property (with getter, setter, and deleter) for both ZipFile and ZipInfo to manage string-based comments safely:
1. ZipFile.comment_text
- get: Unlike member comments, the archive-level comment has no EFS flag in the ZIP specification. The getter should try decoding with UTF-8 first, then fallback to
metadata_encoding(or 'cp437' if not provided). - set: Encodes with
metadata_encoding(or 'cp437' if not provided). Falls back to UTF-8 if encoding fails, optionally issuing a warning. - delete: Clears the comment by resetting the underlying archive comment bytes to
b''.
2. ZipInfo.comment_text
- get: Takes the Unicode comment extra field (
0x6375) if viable. Otherwise, decodes with UTF-8 if the EFS flag is set. Otherwise, uses the boundmetadata_encoding(or 'cp437' if not provided). - set: Encodes with UTF-8 if the EFS flag is set. Otherwise, uses the bound
metadata_encoding(or 'cp437' if not provided). If it fails, it can force the EFS flag and use UTF-8. An even simpler alternative is to always enforce EFS and UTF-8 when modifying via this property. Optionally clear or update the Unicode comment extra field. - delete: Clears the comment by resetting
.commenttob''. Optionally clear the associated Unicode comment extra field.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先检查 ZipFile 和 ZipInfo 现有的注释处理方式以及 metadata_encoding 参数。确定 API 应绑定编码上下文还是使用内部编码属性,然后定义兼容的 comment_text 行为及其边界情况。完成的标准是:选定的设计已实现,并且覆盖了解码、编码、删除、EFS 处理和回退行为。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- tooling
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100