Created
February 21, 2018 14:49
-
-
Save tkfm-yamaguchi/ef36e5a7c4b422314f8f9ec58a02b909 to your computer and use it in GitHub Desktop.
download, unzip and process on memory with Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# HTTP 上の zip ファイルをオンメモリで DL/加工 した結果をファイルに保存. | |
# 行単位での読み出し/加工/書き出しを意識した実装. | |
if __FILE__ == $0 | |
url = "http://localhost:8000/1020201008_00.csv.zip" | |
require "open-uri" | |
require "zip" | |
File.open('conved.csv', 'w') do |output| | |
OpenURI.open_uri(url) do |payload_io| | |
Zip::InputStream.open(payload_io) do |zip_io| | |
zip_io.get_next_entry | |
zip_io.gets # abandon first line (header) | |
while line = zip_io.gets | |
output.puts line.split(',').values_at(0, 4, 5, 7).join(',') | |
end | |
end | |
end | |
# require "net/http" | |
# require "uri" | |
# uri = URI.parse(url) | |
# Net::HTTP.start(uri.host, uri.port) do |http| | |
# # FIXME: IO インターフェイスなオブジェクトで payload を取りたい | |
# response = http.get(uri.path) | |
# | |
# Zip::File.open_buffer(response.body) do |zipfile| | |
# entry = zipfile.first | |
# io = entry.get_input_stream | |
# | |
# io.gets # abandon first line (header) | |
# | |
# while line = io.gets | |
# output.puts line.split(',').values_at(0, 4, 5, 7).join(',') | |
# end | |
# end | |
# end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment