Rails Ziping files - reading binary data

那年仲夏 提交于 2020-01-03 03:08:12

问题


I'm using rubyzip library for zipping files.
But I encounter problems.
I try:

    Zip::ZipOutputStream.open('c:/sites/efiling2/test.zip') do |zos|
        zos.put_next_entry("test.rtf")
        zos.write IO.read('c:/sites/efiling2/test.rtf')
        zos.put_next_entry("test.jpg")
        zos.write IO.read('c:/sites/efiling2/test.jpg')
    end

But write method restricts a size of original files. For example my source file test.jpg has a size of 11913 bytes, but in archive there is a file test.jpg with size 11551 bytes. With test.rtf there is the same situations.

Any suggestions?


回答1:


I suspect your problem may be IO.read(). I'm not so sure it does binary data properly.

I would try this instead and see if it fixes the problem:

File.open(filename, "rb") { |f| f.read }


来源:https://stackoverflow.com/questions/10422075/rails-ziping-files-reading-binary-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!