Rubyzip - unable to load such a file - zip

I am trying to use rubyzip to create zip archives on the fly in my application. I use their readme as a starting point. I added gem to my gemfile:

gem 'rubyzip' 

Then ran bundle install and restarted the rails server.

My controller code:

 require 'rubygems' require 'zip' filename = "#{Time.now.strftime('%y%m%d%H%M%S')}" input_filenames = ["#{filename}.txt"] zip_filename = "#{filename}.zip" Zip::File.open(zip_filename, Zip::File::CREATE) do |zipfile| input_filenames.each do |f| zipfile.add(f, directory + '/' + f) end end 

The error I get: cannot load such file -- zip

I tried require 'zip/zip' , but it throws the same error.

Thanks in advance!

+4
source share
3 answers

You might be looking at a too new example.

If you are using Rubyzip 0.9.x , you need to follow this example :

( require "zip/zip" , then use Zip::ZipFile instead of Zip::File )

 require 'zip/zip' folder = "Users/me/Desktop/stuff_to_zip" input_filenames = ['image.jpg', 'description.txt', 'stats.csv'] zipfile_name = "/Users/me/Desktop/archive.zip" Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile| input_filenames.each do |filename| # Two arguments: # - The name of the file as it will appear in the archive # - The original file, including the path to find it zipfile.add(filename, folder + '/' + filename) end end 
+11
source

I suggest you use:

 gem 'rubyzip', "~> 1.1", require: 'zip' 

and then require 'zip' in your controller should work fine.

+5
source

The rubyzip interface has been changed. You are using the old version. Documents on rubydoc.info from the main branch.

+2
source

Source: https://habr.com/ru/post/1494372/


All Articles