You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何向Zip文件添加注释?求其实现原理及具体方法

How ZIP File Comments Work (And Why Adding a File Might Seem to Add One)

Hey there! I’ve spent plenty of time tinkering with ZIP structures, so let me break this down clearly for you—great catch noticing that adding a file and re-compressing can carry over a comment, but that’s just a side effect of how tools handle ZIP metadata, not because you need a secret hidden file.

Let’s Start With the ZIP File Structure

ZIP files follow a specific layout, and the comment lives in the very last part of the file:

  • Local File Headers + Compressed Data: First, you’ve got each individual file’s metadata (like name, compression method) followed by the actual compressed bytes.
  • Central Directory: Next is a catalog that lists every file in the ZIP—this is what tools use to show you the contents without unpacking everything.
  • End of Central Directory (EOCD) Record: This is the "final boss" of the ZIP structure. It tells tools where the Central Directory starts, how many files are in it, and crucially, it has a dedicated field for the global ZIP file comment. This field can hold up to 65,535 bytes of text or binary data.

Why Adding a File Re-compresses the Comment?

When you open an existing ZIP with a comment, add a new file, and re-save it, your compression tool (like 7-Zip, WinRAR, or even Python’s zipfile module) does two things:

  1. It adds the new file’s data and updates the Central Directory.
  2. It copies the existing comment from the original ZIP’s EOCD into the new ZIP’s EOCD field.
    So the added file isn’t what’s carrying the comment—it’s just that the tool is preserving the comment metadata during the re-packing process.

How to Prove This (No Hidden Files Needed)

You can verify this easily without any special tools:

  • Hex Editor Check: Open a ZIP with a comment in a hex editor (like HxD on Windows or xxd on Linux/macOS). Scroll all the way to the end, and you’ll see your comment text right after the EOCD’s signature (50 4B 05 06—the magic bytes that mark the start of the EOCD).
  • Command Line Check: On Linux/macOS, run unzip -z your_zip_file.zip—this will print the global comment directly. On Windows, use PowerShell with the built-in ZipFile module to read the comment without unpacking.

Add a Comment Without Adding Any Files

If you want to add or modify a ZIP comment directly (no file additions required), you can use a simple Python script:

import zipfile

# Open the ZIP in append mode
with zipfile.ZipFile("your_file.zip", "a") as zip_ref:
    # Set the comment (must be bytes, hence the b prefix)
    zip_ref.comment = b"This is my custom ZIP comment!"

This code directly edits the EOCD’s comment field—no extra files, no re-compressing the whole archive (unless you add files too, of course).

To Sum Up

The comment isn’t stored in a hidden file inside the ZIP—it’s part of the archive’s core structure, tucked away in the final EOCD record. The "add file and re-compress" trick works because your tool is preserving that comment metadata, not because the file itself holds the comment.

内容的提问来源于stack exchange,提问作者jimwan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:18:29