0

i have been using rubyzip for zip/unzip for files/folder ranging from 20MB TO 1GB.i noticed that after zipping a folder of 20MB,the created zipfile is almost of the same size somewhat 20MB.So is rubyzip just zip the file or actually compresses it because the compressed file must be less than 40%-50% of the actual file size.i even tried using system(zip, archive, Dir["#{path}/**/**"]) but i guess i am unable to get the correct syntax to call it.So my questions are

  1. why rubyzip is unable to create an actual zip file which must be less in size too.
  2. for a zipfile of more than 500MB,how van i send it to the client using send_file because its going to cost performance issue for a file of that size.what if i place that zip of 500MB or above in public folder and let the server serve it which might improve the performance,am i correct?
  3. are there any other option instead of using rubyzip/zipruby(which requires libraries too).

I am using ruby 1.9 and rails 2.3.

my code:-

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


def self.compress_test(path)

    path="#{RAILS_ROOT/answers/}"  
     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|
             begin  
                 zipfile.add(file.sub(path+'/',''),file)   
             rescue Zip::ZipEntryExistsError
             end
       end
     end
 end
6
  • Compression ratios depend a lot on the type of file. What type of files are in the folder with total size 20MB? Sep 27, 2013 at 7:07
  • The folder struture is like this parent/child1 child2 and so on upto 100 and child folders will have files that can be xls,zip,txt file where one parent folder will have a collection o folder each of approx 3-5MB.
    – Milind
    Sep 27, 2013 at 7:16
  • If you compress a folder of zip files, the resulting file will not be smaller. Sep 27, 2013 at 7:19
  • Thanks Neil for the quick reply.its not always that there will be zip files.I have created a folder having static images,xls files to test but after compression using above code the size of the zip is almost the same.
    – Milind
    Sep 27, 2013 at 7:26
  • 1
    xls files should compress a little (maybe to 50% of original), but jpg, png or gif will not (they are already compressed inside, you'll get 99% original size or similar) - the images are usually larger files than the spreadsheets, so you probably won't notice in the total file size. Sep 27, 2013 at 7:28

1 Answer 1

1

why rubyzip is unable to create an actual zip file which must be less in size too.

This varies a lot depending on what files you are trying to compress. Text and xls files will compress reasonably well. Media files in formats like JPEG, PNG, MPEG etc are already compressed internally, and often get compression ratios of 99%. They will usually be bigger that other files in the same folder, so the result of compressing a folder with some images, text and spreadsheets will not seem much smaller. Compressing a .zip file can even make the end result larger than you started.

for a zipfile of more than 500MB,how van i send it to the client using send_file because its going to cost performance issue for a file of that size.what if i place that zip of 500MB or above in public folder and let the server serve it which might improve the performance,am i correct?

Yes, saving a large file to disk, and letting the web server send it may be more efficient. The easiest thing to do would be to save the file to a folder where it can be served from, and provide a link. You could also make that from a different server (e.g. a lighttpd instance dedicated to serving out the large files) to avoid loading your application server.

There are some setups that allow you to pass control back from a Ruby process to e.g. Apache ( xsendfile is one I know of ), but setting that up would be a different question, it depends what web server you have, and whether you have security concerns.

are there any other option instead of using rubyzip/zipruby(which requires libraries too).

Probably yes, but it is not likely you will find solutions to your two other questions by changing which gem you are using, because rubyzip is doing a reasonable job here - it is not failing.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.