Subdirectories in Zip file using ZipOutputStream
Figured out the answer, thought I would put it here for search engines to find.
Anyway, the link I posted used
zos.put_next_entry("some-funny-name.jpg")
to add files to the zip file. Turns out, that string parameter isn't just a filename, it can be a path as well! So using
zos.put_next_entry("some-random-folder/some-funny-name.jpg")
Will make your zip file contain a folder called 'some-random-folder', with a file called 'some-funny-name.jpg'.
Opening a multipart/form-data ZIP file with rubyzip
Using
Zip::ZipFile.open(params["zip_file"].path) do |zip_file|
...
end
should work.
rubyzip error when generating zips of images on the fly : End-of-central-directory signature not found
Got it!
It's because Rails 3 defaults to using x_sendfile, which the server isn't setup for.
Nothing to do with zips at all in the end, it's simply file sending. This post explains everything;
Rails sends 0 byte files using send_file
rubyzip coaching
whatever comes inside ||
, considered to be the parameters for the anonymous method (or lambda expression) that comes next,
for ex:
(1..3).each do |n|
puts n
end
can be rewritten as
(1..3).each {|n| puts n}
|n| could be anything, just a name for a variable.
Generate files and download as zip using RubyZip
You should need to close and unlink the zip file like so:
require('zip')
class SomeController < ApplicationController
# ...
def bulk_download
filename = 'my_archive.zip'
temp_file = Tempfile.new(filename)
begin
Zip::OutputStream.open(temp_file) { |zos| }
Zip::File.open(temp_file.path, Zip::File::CREATE) do |zip|
Attachment.all.each do |attachment|
image_file = Tempfile.new("#{attachment.created_at.in_time_zone}.png")
image_file.write(attachment.image_string)
zipfile.add("#{attachment.created_at.in_time_zone}.png", image_file.path)
end
end
zip_data = File.read(temp_file.path)
send_data(zip_data, type: 'application/zip', disposition: 'attachment', filename: filename)
ensure # important steps below
temp_file.close
temp_file.unlink
end
end
end
Here is a good blog post that I used as the source for this code: https://thinkingeek.com/2013/11/15/create-temporary-zip-file-send-response-rails/
Also, it's good practice to keep all your library requirements at the top of the file (i.e. require('zip')
).
Related Topics
Why Does Psych Yaml Interpreter Add Line Breaks Around 80 Characters
Ruby on Rails Looks for CSS in Assets Instead of Public/Stylesheets
What's the Efficient Way to Multiply Two Arrays and Get Sum of Multiplied Values in Ruby
What Are Some Examples of Using Nokogiri
What Purpose Can Anonymous Modules Serve
Generating Sequential Numbers in Multi-User Saas Application
Emacs Ruby Method Parameter Indentation
Should Rbenv Be Installed System-Wide, or at a User Level
Rails Routing: Giving Default Values for Path Helpers
Why Do I Need Asterisk Before an Array
Converting Video to Flash and Mp4 Ruby on Rails
Gem Install Rails Does Not Install Rails
What Do I Need to Do to Get Hash.From_Xml() to Work
How to Find Out Why a Gem Bundle Has Locked a Gem at a Specific Version