Search code examples
rubyrubyzip

rubyzip file order


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).


Solution

  • 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.