我有一项服务可以通过网络传送压缩文件。该 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
最佳答案
theglauber
's answer是正确的。如 the documentation of the IO
class 中所述,这是 File
的父类(super class):
Binary file mode. Suppresses EOL <-> CRLF conversion on Windows. And sets external encoding to ASCII-8BIT unless explicitly specified.
强调我的。在 Windows 上,当以文本模式打开文件时, native 换行符 (\r\n
) 会隐式转换为换行符 (\n
),这可能就是导致腐败。
还有一个事实是IO#puts
确保输出以行分隔符结尾(Windows 上为 \r\n
),这对于二进制文件格式而言是不可取的。
您也没有关闭 File.open
返回的文件。这是一个优雅的解决方案,可以解决所有这些问题:
io.get_output_stream(zip_file_path) do |out|
out.write File.binread(disk_file_path)
end
关于ruby - 为什么二进制文件在压缩时会损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10564283/