列出带有特殊字符(例如表情符号)的 zip 文件条目
我正在编写一个脚本,需要列出 zip 文件中的文件条目。我的问题是,当存在带有表情符号的条目时,CLI 无法正确输出文件名:
❯ zip -r foo.zip test/
adding: test/ (stored 0%)
adding: test/
I'm writing a script that needs to list file entries from a zip file. My problem is that when there is an entry with an emoji, and the CLI doesn't output the file name correctly:
❯ zip -r foo.zip test/
adding: test/ (stored 0%)
adding: test/????.txt (stored 0%)
src on main [!?] is ???? v1.0.0 via ???? v16.14.0
❯ unzip -l foo.zip
Archive: foo.zip
Length Date Time Name
--------- ---------- ----- ----
0 04-08-2022 20:54 test/
0 04-08-2022 20:54 test/�???.txt <---- here is my problem
--------- -------
0 2 files
src on main [!?] is ???? v1.0.0 via ???? v16.14.0
❯ unzip foo.zip test/????.txt
Archive: foo.zip
extracting: test/�???.txt
Is there a way to tell unzip
to list the file entries with consideration of special characters?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎不可能使用
unzip
准确列出 zip 存档中的文件(使用unzip 6.00
进行测试);您必须选择其他工具。我在答案中选择了 Perl,因为它的核心库具有所需的功能。在这里,我使用了
newline
作为分隔符 (-l
),但您应该将其替换为NULL-BYTE
(-l0
) code>) 如果你希望能够 100% 准确地从 bash 读取和处理输出路径:It doesn't seem possible to accurately list the files in a zip archive with
unzip
(tested withunzip 6.00
); you'll have to select an other tool.I chose
perl
in my answer because it has the required functionality in its core library. Here I used anewline
as delimiter (-l
) but you should replace it with aNULL-BYTE
(-l0
) if you want to be able to read and process the outputted paths 100% accurately from bash:remark: Python also have a
ZipFile
module in its core library. I didn't post any Python solution because of the encoding issues of itsstdout
. The fixes aren't compatible between Python versions...