Skip to content

Instantly share code, notes, and snippets.

@hiroshiro
Created February 20, 2014 21:46
Show Gist options
  • Select an option

  • Save hiroshiro/99de509b484aadd84e41 to your computer and use it in GitHub Desktop.

Select an option

Save hiroshiro/99de509b484aadd84e41 to your computer and use it in GitHub Desktop.
Ruby on Rails - Zipファイルを読み込む。Jsonを解析する。
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
@hiroshiro
Copy link
Author

Ruby on Rails - Zipファイルを読み込む。Jsonを解析する。
http://undersourcecode.hatenablog.com/entry/2014/01/22/220329

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment