Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

right way to add an entry from a stream or memory, not a file? with mode STORED? #400

Closed
jrochkind opened this issue Jul 15, 2019 · 1 comment

Comments

@jrochkind
Copy link

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:

  1. 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)
  2. I want to add the entry as STORED, mode not DEFLATE.

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?

@jdleesmiller
Copy link
Member

jdleesmiller commented Jul 20, 2019

Hello, I'm helping out with maintenance.

I think you independently discovered the add_stored method proposed in #366 . So, we can count this as a vote for merging that PR.

It looks like you should be able to pass the (non-)compression method to put_next_entry:

def put_next_entry(entry_name, comment = nil, extra = nil, compression_method = Entry::DEFLATED, level = Zip.default_compression)

I think your point about add not really being a convenience method is correct.

Does that help? Feel free to reopen if not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants