<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>eXpand yOur cReativity &#187; rubyzip</title>
	<atom:link href="http://blog.bhushangahire.net/tag/rubyzip/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bhushangahire.net</link>
	<description></description>
	<lastBuildDate>Thu, 05 Jan 2012 07:17:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Generating ZIP files via Ruby on Rails using rubyzip</title>
		<link>http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=generating-zip-files-via-ruby-on-rails-using-rubyzip</link>
		<comments>http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 09:33:44 +0000</pubDate>
		<dc:creator>Bhushan Ahire</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubyzip]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=65</guid>
		<description><![CDATA[gem install rubyzip Then, in the model that I’m using to generate the zip bundles, I add a couple “require” statements: require 'zip/zip' require 'zip/zipfilesystem' class Album < ActiveRecord::Base (...) end Next, I added a class method called bundle, which when called will use rubygem to generate the zip file. Note: the “permalink” attributes of [...]]]></description>
			<content:encoded><![CDATA[<div class="entry">
<pre>gem install rubyzip</pre>
<p>Then, in the model that I’m using to generate the zip bundles, I add a couple “require” statements:</p>
<pre>require 'zip/zip'
require 'zip/zipfilesystem'

class Album < ActiveRecord::Base
  (...)
end</pre>
<p>Next, I added a class method called bundle, which when called will use rubygem to generate the zip file. Note: the “permalink” attributes of Album and Artist are populated when an object of those models is created. I’m using them because it makes for nice filenames, too.</p>
<pre># create a zipped archive file of all the tracks in an album
def bundle(name = self.permalink, set = self.artist.permalink)
   bundle_filename = "#{RAILS_ROOT}/public/uploads/#{set}-#{name}.zip"

   # check to see if the file exists already, and if it does, delete it.
   if File.file?(bundle_filename)
     File.delete(bundle_filename)
   end

   # set the bundle_filename attribute of this object
   self.bundle_filename = "/uploads/#{set}-#{name}.zip"

   # open or create the zip file
   Zip::ZipFile.open(bundle_filename, Zip::ZipFile::CREATE) {
     |zipfile|
     # collect the album's tracks
     self.tracks.collect {
       |track|
         # add each track to the archive, names using the track's attributes
         zipfile.add( "#{set}/#{track.num}-#{track.filename}", "#{RAILS_ROOT}/public#{track.public_filename}")
       }
   }

   # set read permissions on the file
   File.chmod(0644, bundle_filename)

   # save the object
   self.save
end
</pre>
<p>Next I added a method in my controller:</p>
<pre>def create_bundle
   album = Album.find(params[:id])
   album.bundle
   flash[:notice] = 'Album was successfully zipped.'
   redirect_to album_url(album.artist, album)
end</pre>
<p>And edit my routes.rb accordingly:</p>
<pre>map.create_bundle 'create_bundle/:id', :controller => 'albums', :action => 'create_bundle'</pre>
<p>Now it’s just a matter of creating a link in the view for the admin to click whenever he/she wants to generate the zip file: </p>
<pre><%= link_to('Create Album Zip', create_bundle_path(@album)) %></pre>
<p>…and a link for the user to click to download the zip file if it exists:</p>
<pre><% unless @album.bundle_filename.nil? %>
<div id="grid_right">
    <%= link_to "Download Album Zip", @album.bundle_filename %>
  </div>

<% end %></pre>
<p>That’s it! Refer to the <a href="http://rubyzip.sourceforge.net/">rubyzip documentation</a> for more info.</p>
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

