How can I create a zip file without temporary files in ruby?

ruby, temporary-files, zip

Solution

See the answer at "How can I generate zip file without saving to the disk with Ruby?"

I adapted your example to demonstrate it works.

require 'zip/zip'

zipname = 'test.zip'
File.delete(zipname) if File.exists?(zipname) #delete previous version

stringio = Zip::ZipOutputStream::write_buffer do |zio|
  1.upto(5) do |i| #Just some testfiles with content
    zio.put_next_entry("test#{i}.txt") #Filename
    zio.write("Testcontent %08i" % i)  #generated content
    sleep 1 #sleep some time to see the temporary files
  end
end
stringio.rewind #reposition buffer pointer to the beginning
File.new("test.zip","wb").write(stringio.sysread) #write buffer to zipfile

Problem

I create a zip file with some generated content (in other words: the files in the archive don't exist, the content is build in my script). I use a script similar to this: ``` #~ gem 'rubyzip', '=1.1.0' require 'zip/zip' zipname = 'test.zip' File.delete(zipname) if File.exists?(zipname) #delete previous version Zip::ZipFile.open(zipname, Zip::ZipFile::CREATE) do |zipfile| 1.upto(100) do |i| #Just some testfiles with content zipfile.get_output_stream("%08i.txt" % i) do |output_entry_stream| #Filename output_entry_stream.write("Testcontent %08i" % i) #generated content end sleep 1 #sleep some time to see the temporary files end #testdocs end #ZipFile.open(zipname) ``` This works fine, I get my zip with the correct data inside. But during the zip creation I have a lot of temporary files. They are deleted when the zip is finished, but the files disturb me during the creation. And if the process raises an exception, then I have to delete them manual. I have this behaviour with Zip::VERSION 2.0.2 and 1.1.0 (using the gem rubyzip) - Can I avoid this temporary files? - If not: Can I determine a (temporary) folder for them?

Original source

Related problems