Pinning a directory in Rails

How do I make a zip catalog in ruby ​​on rails? I tried rubyzip without success. I do not need to zip the contents of the directory separately, just archive the directory itself.

+6
source share
2 answers

You will need to iterate over the items in the directory to add an entry to the compressed file.

def compress(path) gem 'rubyzip' require 'zip/zip' require 'zip/zipfilesystem' path.sub!(%r[/$],'') archive = File.join(path,File.basename(path))+'.zip' FileUtils.rm archive, :force=>true Zip::ZipFile.open(archive, 'w') do |zipfile| Dir["#{path}/**/**"].reject{|f|f==archive}.each do |file| zipfile.add(file.sub(path+'/',''),file) end end end 

http://grosser.it/2009/02/04/compressing-a-folder-to-a-zip-archive-with-ruby/

Another way to do this is with the command

 Dir["*"].each do |file| if File.directory?(file) #TODO add OS specific, # 7z or tar . `zip -r "#{file}.zip" "#{file}"` end end 

http://ruby-indah-elegan.blogspot.com/2008/12/zipping-folders-in-folder-ruby-script.html

Update

Thanks Mahmoud Khaled for editing / updating

for the new version use Zip::File.open instead of Zip::ZipFile.open

+12
source

You can create a directory archive with tar tar -cvf your_dir.tar your_dir/

and then compress tar in rails using -

 def gzip_my_dir_tar(your_dir_tar_file) content = File.read(your_dir_tar_file) ActiveSupport::Gzip.compress(content) end 

It has already been answered by Rails 3: How do I create a compressed file on request

0
source

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


All Articles