compress the file

hello

I need to generate a dataset file from webserver and return this file to clients via RestAPI.

the file size is large about 500M+ (plain format like CSV).
do you know how to compress the file after generation and put the compressed content back to clients? And does ruby have a fast random number generator to speed up my content generation?

I am using the lite framework sinatra.

thanks & regards,
ミユナ

Hi ミユナ (Alice),

I need to generate a dataset file from webserver and return this file to clients via RestAPI.

the file size is large about 500M+ (plain format like CSV).
do you know how to compress the file after generation and put the compressed content back to clients? And does ruby have a fast random number generator to speed up my content generation?

To compress the file, you can use Rubyzip. Check the Rubyzip documentation but basically this is what you need

1. Add 'rubyzip' to your Gemfile
2. in your code, do:
require 'zip'

then, you can compress the file src to dest this way:

zip_up(src, zdest)
# set up the compression
Zip.setup do |c|
c.default_compression = Zlib::BEST_COMPRESSION
end

# execute the zip function
Zip::File.open(z_dest, Zip::File::CREATE) do |zipfile|
zipfile.add(File.basename(src_file), src_file)
end

That should be it. You should then be able to serve up the saved output file to the client.

Hope this helps.

Best Regards,
Mohit.
2022-4-23 | 3:10 pm.

···

On 2022-4-23 9:51 am, ミユナ (alice) wrote: