Sam Roberts
Sam Roberts

Reputation: 185

Opening a zip file that contains a csv file to use in a rake task

I have a rake task that looks like this

require 'open-uri'
require 'csv'
namespace :Table do
   task reload: :environment do
       ActiveRecord::Base.connection.execute("TRUNCATE Table RESTART IDENTITY")
       csv_text = open('URL')
       csv = CSV.parse(csv_text, :headers=>true)
       csv.each do |row|
         Table.create(table columns)
       end
   end
end

Now i can either have the file come back as gzip or zip. It has to be compressed however the csv is the only file inside the zip file.

Any ideas how i can make this open the csv inside the zip file?

Thanks for the help

Sam

Upvotes: 1

Views: 1715

Answers (2)

Mike K.
Mike K.

Reputation: 3789

I would use the rubyzip gem, read the file down to a temp file unzip it, pull the file you want and open it normally as a local file for the CSV.

require 'open-uri'
require 'csv'
require 'zip'
namespace :Table do
   task reload: :environment do
       ActiveRecord::Base.connection.execute("TRUNCATE Table RESTART IDENTITY")

       t=Tempfile.new("foo.zip")
       url = open("url")
       t.binmode
       t.write stringIo.read
       t.close

       zip_file = Zip::File.open(t.path)
       entry = zip_file.glob('*.csv').first

       csv_text = entry.get_input_stream.read
       csv = CSV.parse(csv_text, :headers=>true)
       csv.each do |row|
         Table.create(table columns)
       end
   end
end

Upvotes: 1

Kristján
Kristján

Reputation: 18813

Ruby core includes Zlib support. If you've got a URL pointing to a gzipped file, you can use Zlib::GzipReader to inflate the Tempfile returned by open.

gzipped = open('URL')
csv_text = Zlib::GzipReader.new(gzipped).read
csv = CSV.parse(csv_text, :headers=>true)

Upvotes: 4

Related Questions