Cod*_*ike 6 ruby zip ruby-on-rails
我有一个Ruby应用程序,我需要修改现有的zip文件.
我想在内存中构建zip文件并流回字节而无需将文件写入文件系统.如果我最终在Heroku上托管这个,我不认为我可以写入文件系统.有谁知道这样做的方法?
我看了Zip :: ZipFile,但看起来总是想写入文件系统.我认为"基于java实现"我将能够获得压缩文件的字节,你可以在java中做,但我没有看到这样做的方法.
编辑:
我要问的基本上与此相同,但对于Ruby而不是Python: 函数创建内存中的zip文件并作为http响应返回
这是一篇讨论此问题的博客文章。它使用Tempfile,对我来说似乎是一个很好的解决方案(尽管阅读评论以获取一些有用的附加讨论)。
一个例子,来自帖子:
def download_zip(image_list)
if !image_list.blank?
file_name = "pictures.zip"
t = Tempfile.new("my-temp-filename-#{Time.now}")
Zip::ZipOutputStream.open(t.path) do |z|
image_list.each do |img|
title = img.title
title += ".jpg" unless title.end_with?(".jpg")
z.put_next_entry(title)
z.print IO.read(img.path)
end
end
send_file t.path, :type => 'application/zip',
:disposition => 'attachment',
:filename => file_name
t.close
end
end
Run Code Online (Sandbox Code Playgroud)
这个解决方案应该与 Heroku 配合得很好。