LoginSignup
14
11

More than 5 years have passed since last update.

RubyでZipファイルを解凍するときのサンプルコード

Last updated at Posted at 2015-11-17

コード内容

sample.zip内のファイルをすべて、outディレクトリに解凍する。

サンプルコード

sample.rb

require 'zip'

dest = 'out/'

Zip::File.open('sample.zip') do |zip|
  zip.each do |entry|
    p entry.name
    # { true } は展開先に同名ファイルが存在する場合に上書きする指定
    zip.extract(entry, dest + entry.name) { true }
  end
end

ソース

ドキュメント

rubyzipのドキュメント
http://www.rubydoc.info/github/rubyzip/rubyzip/master/frames

補足

Zip解凍中に"ディレクトリが存在しない"とエラーが発生する現象の対応

rubyzipがZipファイルの解凍時にディレクトリ作成よりも
ファイル出力を先に実行する場合がある。
対応策をsample_bugfix.rbに記載した。

14
11
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11