列出带有特殊字符(例如表情符号)的 zip 文件条目

发布于 2025-01-20 02:06:48 字数 184 浏览 4 评论 0原文

我正在编写一个脚本,需要列出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼疼热情 2025-01-27 02:06:48

似乎不可能使用 unzip 准确列出 zip 存档中的文件(使用 unzip 6.00 进行测试);您必须选择其他工具。

我在答案中选择了 Perl,因为它的核心库具有所需的功能。在这里,我使用了 newline 作为分隔符 (-l),但您应该将其替换为 NULL-BYTE (-l0) code>) 如果你希望能够 100% 准确地从 bash 读取和处理输出路径:

perl -l -e '
    use IO::Uncompress::Unzip;
    $zip = IO::Uncompress::Unzip->new($ARGV[0]);
    while($zip->nextStream()) {
        print $zip->getHeaderInfo()->{Name}
    }
' foo.zip
test/
test/

It doesn't seem possible to accurately list the files in a zip archive with unzip (tested with unzip 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 a newline as delimiter (-l) but you should replace it with a NULL-BYTE (-l0) if you want to be able to read and process the outputted paths 100% accurately from bash:

perl -l -e '
    use IO::Uncompress::Unzip;
    $zip = IO::Uncompress::Unzip->new($ARGV[0]);
    while($zip->nextStream()) {
        print $zip->getHeaderInfo()->{Name}
    }
' foo.zip
test/
test/????.txt

remark: Python also have a ZipFile module in its core library. I didn't post any Python solution because of the encoding issues of its stdout. The fixes aren't compatible between Python versions...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文