Description
I'm getting a bit confused about the rubyzip api.
I am creating a zip file, and want to add an entry to it where:
- I do not have a local File for the contents I want to add. I have the data in memory (or in an IO stream object that is not a
File
) - I want to add the entry as
STORED
, mode notDEFLATE
.
For the first criteria, not having a local file, I think this would work:
Zip::OutputStream.open('exampleout.zip') do |zos|
zos.put_next_entry(desired_filename)
zos.puts some_binary_content
end
For the second criteria, STORED
rather than DEFLATE
, I figured out with difficulty this:
entry = ::Zip::Entry.new(zipfile.name, desired_filename, nil, nil, nil, nil, ::Zip::Entry::STORED)
zipfile.add(entry, File.open("something"))
But these are entirely different APIs. For the OutputStream
API in the first example... there doens't seem to be any way to specify STORED
rather than DEFLATE
?
For the Zip::Entry
/add API in the second example... there doesn't seem to be any way to pass it something that isn't a real file object. If I pass it a string to zipfile.add(entry, "foo bar")
, I get No such file or directory @ rb_file_s_lstat - foo bar
.
Is there any API that does both things? Looking at the source for the #add method, it's described as a "convenience method", so maybe I can do just what it's doing myself in more steps -- but it's implementation accesses the @entry_set
iVar, so I can't do just what it does in my own caller code....
Is there any API that has all the features I need?