如何在ruby中的内存中创建zip文件? [英] How to create zip file only in memory in ruby?

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

问题描述

我想创建一个给定目录中所有HTML文件的zip文件。邮件将作为附件通过电子邮件发送,以及目录中的其余文件。到目前为止,我尝试过的所有电子邮件客户端在阅读电子邮件时遇到困难,如果任何附件是HTML文件,如果我从小马发送邮件。所以我想我会将它们压缩。



有没有办法 zip 动态显示HTML 文件但仅在内存中 ,而不在 Windows平台上使用任何临时文件?最好不要使用任何外部程序?



如果我理解正确,两种方法描述如下:



正在使用某种临时文件。 >解决方案

起初,我想也许你可以使用StringIO对象来写入,但它看起来像ZipOutputStream类坚持打开临时文件。



如果您不介意调用外部程序,可以通过使用 - 而不是文件名将标准linux zip发送到标准输出而不是文件。因此,只需将输出收集到一个变量中,然后按照自己喜欢的方式进行操作即可。

  zipdata =%x(zip -q  -  * .html)

我认为这不会仅仅是使用临时效率文件,但你必须测量它确定。


I want to create a zip file of all HTML files in a given directory. The zip will be sent as an attachment via email along with the rest of the files from the directory. All email clients I tried so far have trouble reading an email if any attachment is a HTML file, if I send from pony. So I thought I'd zip them.

Is there any way to zip the HTML files on the fly but really only in the memory, without using any temp files on Windows platform? Preferably not using any external program?

If I understood right, both methods described in:

are using some kind of temp file.

解决方案

At first I thought maybe you could use a StringIO object to write to, but it looks like the ZipOutputStream class insists on opening a temporary file.

If you don't mind calling an external program, standard linux zip can be told to send otuput to stdout instead of a file by using "-" instead of a filename. So just collect the output into a variable then do whatever you like with it.

zipdata = %x(zip -q - *.html)

I don't think this is going to be an efficiency gain over just using a temporary file, but you'd have to measure it to be sure.

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

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