为什么二进制文件在压缩时会损坏?

Hél*_*nda 4 ruby windows zip heroku binary-data

我有一项通过网络提供压缩文件的服务。该 zip 包含 Windows 平台的可执行文件。

我正在使用 RubyZip 库来压缩文件,但该过程会损坏二进制文件。在我的本地服务器上,我们通过系统调用使用 zip 命令,它工作正常。

zip 命令在 Heroku 上不可用,我根本没有选择。

我正在使用这个类:

require 'zip/zip'

# This is a simple example which uses rubyzip to
# recursively generate a zip file from the contents of
# a specified directory. The directory itself is not
# included in the archive, rather just its contents.
#
# Usage:
#   directoryToZip = "/tmp/input"
#   outputFile = "/tmp/out.zip"   
#   zf = ZipFileGenerator.new(directoryToZip, outputFile)
#   zf.write()
class ZipFileGenerator

  # Initialize with the directory to zip and the location of the output archive.
  def initialize(inputDir, outputFile)
    @inputDir = inputDir
    @outputFile = outputFile
  end

  # Zip the input directory.
  def write()
    entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..") 
    io = Zip::ZipFile.open(@outputFile, Zip::ZipFile::CREATE); 

    writeEntries(entries, "", io)
    io.close();
  end

  # A helper method to make the recursion work.
  private
  def writeEntries(entries, path, io)

    entries.each { |e|
      zipFilePath = path == "" ? e : File.join(path, e)
      diskFilePath = File.join(@inputDir, zipFilePath)
      puts "Deflating " + diskFilePath
      if  File.directory?(diskFilePath)
        io.mkdir(zipFilePath)
        subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..") 
        writeEntries(subdir, zipFilePath, io)
      else
        io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())}
      end
    }
  end

end
Run Code Online (Sandbox Code Playgroud)

Mat*_*ira 5

theglauber的答案是正确的。正如该类(其超类)的文档IO中所述File

二进制文件模式。禁止 Windows 上的 EOL <-> CRLF 转换。除非明确指定,否则将外部编码设置为 ASCII-8BIT。

强调我的。在 Windows 上,当以文本模式打开文件时,本机行结尾\r\n( ) 会隐式转换为换行符 ( ),这可能是导致损坏的原因。\n

还有一个事实是,IO#puts确保输出以行分隔符结束(\r\n在 Windows 上),这对于二进制文件格式来说是不可取的。

您也没有关闭 . 返回的文件File.open。这是一个优雅的解决方案,可以解决所有这些问题:

io.get_output_stream(zip_file_path) do |out|
  out.write File.binread(disk_file_path)
end
Run Code Online (Sandbox Code Playgroud)