tags:

views:

583

answers:

1

Hi,

I'm struggling with getting rubyzip to append directories to a zipoutputstream. (I want the output stream so I can send it from a rails controller). My code follows this example:

http://info.michael-simons.eu/2008/01/21/using-rubyzip-to-create-zip-files-on-the-fly/

When modified to include directories in the list of files to add I get the following error:

Any help would be greatly appreciated.

fturtle

UPDATE

After trying a number of solutions I had best success with zipruby which has a clean api and good examples: http://zipruby.rubyforge.org/.

+1  A: 

OOOOOuuuhh...you DEFINITELY want ZIPPY. It's a Rails plugin that abstracts a lot of the complexity in rubyzip, and lets you create what you're talking about, including directories (from what I recall).

Here you go:

http://github.com/toretore/zippy

And direct from the zippy site:

Example controller:
def show
  @gallery = Gallery.find(params[:id])
  respond_to do |format|
    format.html
    format.zip
  end
end

Example view:
zip['description.txt'] = @gallery.description
@gallery.photos.each do |photo|
  zip["photo_#{photo.id}.png"] = File.open(photo.url)
end

edit: Amending per user comment:

Hmm...the whole objective of using Zippy is to make it a whole lot easier to use ruby zip. Ya might want to take a second (or first) look...

Here's how to make a directory with directories:

some_var = Zippy.open('awsum.zip') do |zip|
  %w{dir_a dir_b dir_c diri}.each do |dir|  
    zip["bin/#{dir}/"]
  end
end

...

send_file some_var, :file_name => ...
btelles
Thanks this looks good but the docs are a little shy and I haven't the time to delve through the source just now. So, for example, how would I create a zip stream and add a directory of directories? Also, I need to use sendfile rather than a custom mime type. Thanks.
fturtle
fixed up the answer in case you're interested.
btelles
Sorry, but I've found this gem to be quite hard work. There's a couple of others I'm going to try that can stream a newly created zip in the manner I want.
fturtle
Coolio...good luck! :-)
btelles