我有一个通过networking提供压缩文件的服务。 该zip包含Windows平台的可执行文件。
我正在使用RubyZip库来压缩文件,但是这个过程破坏了二进制文件。 在我的本地服务器,我们通过系统调用使用zip命令,它工作正常。
Heroku的zip命令不可用,而且我只是没有select。
我正在使用这个类:
用PythonparsingNginx的http_userid_module cookie
char *和unsigned char *转换
在0x1A上读取扼stream线
为什么我无法将所有数据从sys.stdin写入Windows中的文件?
直接从Windows剪贴板获取二进制数据
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
深入了解二进制文件
将字节转换为可读的内容
Linux X86-64汇编和printf
theglauber的答案是正确的。 正如File的超类IO类的文档中所述 :
二进制文件模式。 在Windows上禁止EOL < – > CRLF转换 。 除非明确指定,否则将外部编码设置为ASCII-8BIT。
强调我的。 在Windows上,当以文本模式打开文件时,本机行结束符( rn )被隐式转换为换行符( n ),这可能是造成损坏的原因。
还有一个事实是, IO#puts确保输出以行分隔符结束(在Windows上是rn ),这对于二进制文件格式是不理想的。
您也不关闭File.open返回的文件。 这是一个优雅的解决方案,可以解决所有这些问题:
io.get_output_stream(zip_file_path) do |out| out.write File.binread(disk_file_path) end
如果这是Windows,则可能必须以二进制模式打开输出文件。
例如: io = File.open('foo.zip','wb')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。