Created
February 20, 2014 21:46
-
-
Save hiroshiro/99de509b484aadd84e41 to your computer and use it in GitHub Desktop.
Ruby on Rails - Zipファイルを読み込む。Jsonを解析する。
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
| require 'zip' | |
| require 'json' | |
| require 'kconv' | |
| module ZipData | |
| def self.read(file_path) | |
| content = get_file_content(file_path) | |
| json = JSON.parser.new(content) | |
| return json.parse() | |
| end | |
| private | |
| def self.get_file_content(file_path) | |
| bindata = {} | |
| Zip::File.open(file_path) do |zipfile| | |
| zipfile.each do |f| | |
| name = f.name | |
| size = f.size | |
| next if name =~ /\/$/ # ディレクトリは除外 | |
| bindata[name] = zipfile.read(f.name) | |
| end | |
| end | |
| content = bindata[File.basename(file_path, '.zip') + '.json'] | |
| return content.toutf8() | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ruby on Rails - Zipファイルを読み込む。Jsonを解析する。
http://undersourcecode.hatenablog.com/entry/2014/01/22/220329