Rails Ziping文件-读取二进制数据 [英] Rails Ziping files - reading binary data
本文介绍了Rails Ziping文件-读取二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 rubyzip 库来压缩文件.
但是我遇到了问题.
我尝试:
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
但是write方法会限制原始文件的大小.例如,我的源文件 test.jpg 的大小为 11913字节,但是在存档中,文件 test.jpg 的大小为 11551字节.使用test.rtf时,情况相同.
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.
有什么建议吗?
推荐答案
我怀疑您的问题可能是IO.read()
.我不确定它是否可以正确执行二进制数据.
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 }
这篇关于Rails Ziping文件-读取二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文