Wednesday, April 18, 2012

How do I create a zip file of a given compressed size in rails

I have a pile of records that need to be converted to XML then zipped up into a file, so I can send it on to a server that is expecting said records.
The problem I have, is that the server can only accept files that are smaller than a given amount.. Lets say for argument sake 10 Megs



    require 'zip/zip'    
Zip::ZipOutputStream.open("tmp/myfile_#{Process.pid}.zip") do |zos|
i_xml.each_with_index do |xml, index|
zos.put_next_entry("#{index}.xml")
zos << xml
end
end


The code above creates the zip file perfectly.. but I don't see how I can get the compressed size.



I can give some lea-way for the zip header and stuff.. So once I can tell how big my output is, I could tinker. It's just getting that size seems not in the cards for this class.



Note: I've tried installing zipRuby because it's has a compressed size method, but that just leads me down another rabbit hole.. Native extensions and such.





No comments:

Post a Comment