3
votes

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.

1
Why is a temp file undesirable? It's common to use them behind the scenes for temporary storage.the Tin Man
It is an option to use temp file if I cannot do it only in memory. It's ok to use temp file but I prefer not to.Radek
The reason why a temp file is advantageous is in the case that the archive grows big enough to impact RAM. Spooling to a temp file will keep the machine running longer since it's a lot less likely to consume all the available disk space than it is to consume all available RAM. If you are on a machine with little free RAM give serious consideration to the temp file.the Tin Man
It's more the other way around. The zip will contain mostly only two small HTML files. So I thought it would easier and faster to do everything in memory.Radek
For a small zip it will be marginally faster after you factor in buffering. I doubt you'd notice much difference.the Tin Man

1 Answers

1
votes

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.