3

我想在给定目录中创建所有 HTML 文件的 zip 文件。zip 将与目录中的其余文件一起作为附件通过电子邮件发送。到目前为止,如果我从小马发送任何附件是 HTML 文件,我尝试的所有电子邮件客户端都无法阅读电子邮件。所以我想我会把它们拉上拉链。

有什么方法可以即时压缩HTML文件,但实际上只在内存中,而不使用Windows 平台上的任何临时文件?最好不要使用任何外部程序?

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

正在使用某种临时文件。

4

1 回答 1

1

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.

于 2012-07-18T01:16:45.467 回答