Using rubyzip to add files and nested directories to a zipoutputstream
Asked Answered
W

4

8

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.

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/.

Wonky answered 24/2, 2010 at 0:48 Comment(1)
Great job finding zipruby, saved my day!Hype
E
9
Zip::ZipFile.open(path, Zip::ZipFile::CREATE) do |zip|
  songs.each do |song|
    zip.add "record/#{song.title.parameterize}.mp3", song.file.to_file.path
  end
end
Eurhythmy answered 11/11, 2010 at 9:18 Comment(0)
Y
3

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 => ...
Younts answered 24/2, 2010 at 1:50 Comment(5)
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.Wonky
fixed up the answer in case you're interested.Younts
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.Wonky
@btelles, Zippy looks nice but there really are no docs for it. Do you happen to know any links to sample code? Your example of creating (empty) directories is a good start but what I want to do is recursively COPY a directory full of existing files and subdirectories. I haven't yet found examples of how to even copy a single file (only how to write new ones).Lelia
Sorry, I don't know of any links, however you may find the specs at least a little helpful. :-/Younts
L
3

Zippy will work for this. There may be a more cool way to do this but since there are essentially no docs, here's what I came up with for recursively copying directories with Zippy in a Rakefile. This Rakefile is used in a Rails environment so I put gem requirements in my Gemfile:

#Gemfile
source 'http://rubygems.org'
gem 'rails'
gem 'zippy'

And this is the Rakefile

#Rakefile
def add_file( zippyfile, dst_dir, f )
  zippyfile["#{dst_dir}/#{f}"] = File.open(f)
end

def add_dir( zippyfile, dst_dir, d )
  glob = "#{d}/**/*"
  FileList.new( glob ).each { |f|
    if (File.file?(f))
      add_file zippyfile, dst_dir, f
    end
  }
end

task :myzip do
  Zippy.create 'my.zip' do |z|
    add_dir z, 'my', 'app'
    add_dir z, 'my', 'config'
    #...
    add_file z, 'my', 'config.ru'
    add_file z, 'my', 'Gemfile'
    #...
  end
end

Now I can use it like this:

C:\> cd my
C:\my> rake myzip

and it will produce my.zip which contains an inner directory called 'my' with copies of selected files and directories.

Lelia answered 18/5, 2011 at 20:56 Comment(0)
R
3

I was able to get directories working with the same ZipOutputStream used in the original article.

All I had to do was add the directory when calling zos.put_next_entry.

For example:

require 'zip/zip'
require 'zip/zipfilesystem'

t = Tempfile.new("some-weird-temp-file-basename-#{request.remote_ip}")
# Give the path of the temp file to the zip outputstream, it won't try to open it as an archive.
Zip::ZipOutputStream.open(t.path) do |zos|
  some_file_list.each do |file|
    # Create a new entry with some arbitrary name
    zos.put_next_entry("myfolder/some-funny-name.jpg") # Added myfolder/
    # Add the contents of the file, don't read the stuff linewise if its binary, instead use direct IO
    zos.print IO.read(file.path)
  end
end
# End of the block  automatically closes the file.
# Send it using the right mime type, with a download window and some nice file name.
send_file t.path, :type => 'application/zip', :disposition => 'attachment', :filename => "some-brilliant-file-name.zip"
# The temp file will be deleted some time...
t.close

I just changed zos.put_next_entry('some-funny-name.jpg') to zos.put_next_entry('myfolder/some-funny-name.jpg'), and the resulting zipfile had a nested folder called myfolder that contained the files.

Riband answered 23/4, 2014 at 19:24 Comment(1)
Your method worked nicely, thanks! I almost switched to zipruby, but it doesn't seem to be maintained anymore.Jevons

© 2022 - 2024 — McMap. All rights reserved.