如何仅在内存中修改 zip 文件? [英] How can I modify a zip file in memory only?

查看:72
本文介绍了如何仅在内存中修改 zip 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Ruby 应用程序,我需要修改现有的 zip 文件.

I have a Ruby application, and I need to modify an existing zip file.

我想在内存中构建 zip 文件并流回字节,而无需将文件写入文件系统.如果我最终在 Heroku 上托管它,我认为我无法写入文件系统.有谁知道这样做的方法吗?

I want to build the zip file in memory and stream back the bytes without ever writing the file to the filesystem. If I end up hosting this on Heroku I don't think I can write to the filesystem. Does anyone know of a way to do that?

我查看了 Zip::ZipFile 但它看起来总是这样想写入文件系统.我认为基于 java 实现"我将能够获取压缩文件的字节,这可以在 java 中完成,但我没有看到这样做的方法.

I looked at Zip::ZipFile but it looks like it always wants to write to the filesystem. I figured being "based on the java implementation" I would be able to just get the compressed file's bytes, which you can do in java, but I don't see a way to do that.

我要问的基本上与此相同,但是对于 Ruby 而不是 Python:创建内存压缩文件的函数并作为 http 响应返回

What I am asking is basically the same as this, but for Ruby instead of Python: Function to create in-memory zip file and return as http response

推荐答案

有同样的问题,必须通过关闭文件并读取数据并将其作为 send_data 流式传输来使其工作

had same issue, got to get it work by closing the file and reading the data and streaming it as send_data

然后找到了另一个在 heroku 上运行良好并且可以处理内存缓冲区的库:它是 zipruby(不是 ruby​​zip).

then found another library that works fine on heroku and can handle with in-memory buffers: it's zipruby (not rubyzip).

buffer = ''
Zip::Archive.open_buffer(buffer, Zip::CREATE) do |archive|
  files.each do |wood, report|
    title = wood.abbreviation+".txt"
    archive.add_buffer(title, report);
  end
end
file_name = "dimter_#{@offer.customerName}_#{Time.now.strftime("%m%d%Y_%H%M")}.zip"
send_data buffer, :type => 'application/zip', :disposition => 'attachment', :filename => file_name

这篇关于如何仅在内存中修改 zip 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆