我有一項通過網絡傳輸壓縮文件的服務。該zip包含Windows平臺的可執行文件。爲什麼壓縮二進制文件時會損壞?
我正在使用RubyZip庫來壓縮文件,但該進程破壞了二進制文件。在我的本地服務器上,我們通過系統調用使用zip命令,它工作正常。
在Heroku中zip命令不可用,而且我簡直沒有選擇。
我使用這個類:
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。我想我太渴望RTFM :) Valeu! –