rubyzip 文件顺序

发布于 2024-11-14 18:57:51 字数 549 浏览 7 评论 0原文

我正在为一个目录制作存档,其中每个文件都有一个从 001 到 049 开始的 3 位数字的名称。我的代码如下:

Zip::ZipFile.open(File.join(out, dir+".cbz"), Zip::ZipFile::CREATE) {
  |zipfile|
  Dir.glob(File.join(dir, "*")).sort.each {
    |file|
    puts "add file #{file}"
    zipfile.add(File.basename(file),file)
  }
}

添加文件时,我使用 puts 验证它们是否已添加按数字升序排列。 但是当我尝试使用 zip 解压缩它们时,我发现它们是以随机顺序解压缩的(例如 045、002,...)。

如何确保它们按数字顺序解压?

编辑:使用十六进制编辑器查看生成的 zip 文件,发现图像的添加顺序不合理,但它们是通过 unzip 按照它们在中央目录中出现的顺序提取的( zip 文件末尾的块)。

I am making an archive for a directory, where each file has a name with 3 digits starting from 001 to 049. My code is the following:

Zip::ZipFile.open(File.join(out, dir+".cbz"), Zip::ZipFile::CREATE) {
  |zipfile|
  Dir.glob(File.join(dir, "*")).sort.each {
    |file|
    puts "add file #{file}"
    zipfile.add(File.basename(file),file)
  }
}

When adding the files, I verified with puts that they are added in numerically ascending order.
But when I try to decompress them with zip, I found that they are decompressed in a random order (eg. 045, 002, ...).

How can I ensure that they decompress in the numerical order?

Edit: Taking a look at the produced zip file with a hex editor shows that the images are added in no sensible order, but that they are extracted via unzip in the order they a present in the central directory (block at the end of the zip file).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

酒解孤独 2024-11-21 18:57:51

我已将此问题追溯到 ruby​​zip 库中的一个问题,即条目数组在写入中央目录之前未进行排序,但 unzip 依赖于此顺序。

已修复,并向上游发送了拉取请求。

I have traced this to an issue in the rubyzip library, whereby the entries array was not being sorted prior to being written to the central directory, but unzip was dependent on this order.

Fixed, and sent a pull request upstream.

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