使用libzip自带的ziptool工具查看和修改zip文件信息

libzip的src目录下包括ziptool.c,在build目录下make之后,它的二进制文件保存在build/src目录下。
以下是它的帮助信息

./ziptool -h
usage: ./ziptool [-ceghnrst] [-l len] [-o offset] archive command1 [args] [command2 [args] ...]

Supported options are:
	-c		check consistency
	-e		error if archive already exists (only useful with -n)
	-g		guess file name encoding (for stat)
	-h		display this usage
	-l len		only use len bytes of file
	-n		create archive if it doesn't exist
	-o offset	start reading file at offset
	-r		print raw file name encoding without translation (for stat)
	-s		follow file name convention strictly (for stat)
	-t		disregard current archive contents, if any

Supported commands and arguments are:
	add name content
	    add file called name using content

	add_dir name
	    add directory

	add_file name file_to_add offset len
	    add file to archive, len bytes starting from offset

	add_from_zip name archivename index offset len
	    add file from another archive, len bytes starting from offset

	cat index
	    output file contents to stdout

	cat_partial index start length
	    output partial file contents to stdout

	count_extra index flags
	    show number of extra fields for archive entry

	count_extra_by_id index extra_id flags
	    show number of extra fields of type extra_id for archive entry

	delete index
	    remove entry

	delete_extra index extra_idx flags
	    remove extra field

	delete_extra_by_id index extra_id extra_index flags
	    remove extra field of type extra_id

	get_archive_comment 
	    show archive comment

	get_archive_flag flag
	    show archive flag

	get_extra index extra_index flags
	    show extra field

	get_extra_by_id index extra_id extra_index flags
	    show extra field of type extra_id

	get_file_comment index
	    get file comment

	get_num_entries flags
	    get number of entries in archive

	name_locate name flags
	    find entry in archive

	print_progress 
	    print progress during zip_close()

	rename index name
	    rename entry

	replace_file_contents index data
	    replace entry with data

	set_archive_comment comment
	    set archive comment

	set_archive_flag flag
	    set archive flag

	set_extra index extra_id extra_index flags value
	    set extra field

	set_file_comment index comment
	    set file comment

	set_file_compression index method compression_flags
	    set file compression method

	set_file_dostime index time date
	    set file modification time and date (DOS format)

	set_file_encryption index method password
	    set file encryption method

	set_file_mtime index timestamp
	    set file modification time

	set_file_mtime_all timestamp
	    set file modification time for all files

	set_password password
	    set default password for encryption

	stat index
	    print information about entry


Supported flags are:
	0	(no flags)
	4	ZIP_FL_ENC_CP437
	8	ZIP_FL_ENC_UTF_8
	C	ZIP_FL_NOCASE
	c	ZIP_FL_CENTRAL
	d	ZIP_FL_NODIR
	l	ZIP_FL_LOCAL
	r	ZIP_FL_ENC_RAW
	s	ZIP_FL_ENC_STRICT
	u	ZIP_FL_UNCHANGED

Supported archive flags are:
	create-or-keep-empty-file-for-archive
	is-torrentzip
	rdonly
	want-torrentzip

Supported compression methods are:
	default
	deflate
	store
	xz
	zstd

Supported encryption methods are:
	none
	AES-128
	AES-192
	AES-256
	PKWARE

The index is zero-based.

可见,它的功能主要是:

1.对已有zip文件相关元数据和数据的查看,一般以get开头
2.往zip文件中添加注释、元数据等,一般以set开头
3.往zip文件中添加条目,以add开头
4.删除zip文件中的条目,一般以delete开头

举例如下:(注意第一个参数必须是有效的zip文件名,不然会出一些奇怪的问题)

#添加压缩包文件注释并查看
./ziptool /shujv/par/top5.zip set_archive_comment 'zip format using zstd'
./ziptool /shujv/par/top5.zip get_archive_comment 
Archive comment: zip format using zstd

#添加条目文件注释并查看,1是文件索引号
./ziptool /shujv/par/top5.zip set_file_comment 1 "this file is an xlsx"
./ziptool /shujv/par/top5.zip get_file_comment 1 
File comment for 'docProps/app.xml': this file is an xlsx

#查看条目元数据,1是文件索引号,93是zstd算法的代号
./ziptool /shujv/par/top5.zip stat 1
name: 'docProps/app.xml'
index: '1'
size: '205'
compressed size: '163'
mtime: 'Wed Aug 27 2025 16:00:44'
crc: '484dc746'
compression method: '93'
encryption method: '0'

#查看条目文件部分数据,1是文件索引号
./ziptool /shujv/par/top5.zip cat_partial 1 1 50
Properties xmlns="http://schemas.openxmlformats.or

#查看一个xls文件改名的zip文件,报错
./ziptool /shujv/par/menu.xls.zip stat 0
can't open zip archive '/shujv/par/menu.xls.zip': Not a zip archive

### 如何使用 libzip 编译生成的 ziptool 压缩文件 通过 libzip 编译生成的 `ziptool` 是一个命令行工具,用于处理 ZIP 文件的创建、添加、删除提取等操作。以下是如何使用 `ziptool` 进行文件压缩的详细说明。 #### 1. 使用 `ziptool` 创建新的 ZIP 文件 若要创建一个新的 ZIP 文件并添加文件到其中,可以使用以下命令: ```bash ziptool create archive.zip file1.txt file2.txt ``` 此命令将创建名为 `archive.zip` 的 ZIP 文件,并将 `file1.txt` `file2.txt` 添加到其中[^1]。 #### 2. 向现有的 ZIP 文件中添加文件 如果需要向已有的 ZIP 文件中添加新文件,可以使用以下命令: ```bash ziptool add archive.zip file3.txt ``` 此命令会将 `file3.txt` 添加到现有的 `archive.zip` 中[^1]。 #### 3. 替换 ZIP 文件中的现有文件 如果需要替换 ZIP 文件中的某个文件,可以先删除该文件,然后再添加新的版本: ```bash ziptool delete archive.zip oldfile.txt ziptool add archive.zip newfile.txt ``` 上述命令首先从 `archive.zip` 中删除 `oldfile.txt`,然后添加 `newfile.txt`。 #### 4. 压缩多个文件或目录 若需要压缩多个文件或整个目录,可以递归地添加所有文件: ```bash ziptool add archive.zip path/to/directory/* ``` 这将把 `path/to/directory/` 下的所有文件添加到 `archive.zip` 中[^1]。 #### 5. 查看 ZIP 文件的内容 在压缩之前或之后,可以查看 ZIP 文件中的内容以确认操作是否成功: ```bash ziptool list archive.zip ``` 此命令将列出 `archive.zip` 中的所有文件及其相关信息。 --- ### 示例代码:编译并运行 ziptool 以下是编译运行 `ziptool` 的完整流程示例: #### 编译步骤 确保已安装 libzip 库后,可以通过以下命令编译 `ziptool`: ```bash gcc -o ziptool ziptool.c -lzip ``` #### 使用示例 假设当前目录下有文件 `file1.txt` `file2.txt`,可以执行以下命令来创建 ZIP 文件: ```bash ./ziptool create archive.zip file1.txt file2.txt ``` 验证 ZIP 文件内容: ```bash ./ziptool list archive.zip ``` --- ### 注意事项 - 确保系统已正确安装 libzip 库及其开发头文件。 - 如果遇到错误,请检查是否遗漏了 `-lzip` 参数,或者 libzip 库未正确安装。 - 在删除文件时,注意索引位置的准确性,否则可能导致意外行为。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值