How to create zip file only in memory in ruby?

后端 未结 1 1832
清歌不尽
清歌不尽 2021-01-21 08:24

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 emai

相关标签:
1条回答
  • 2021-01-21 08:58

    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.

    0 讨论(0)
提交回复
热议问题