zip ファイル作成中に Too many open files (Zip::Error)

zipruby の続き。200超のファイルをzipに追加していたら、こんなエラーが出た。(T^T)

Close archive failed: Failure to create temporary file: 
Too many open files (Zip::Error)


エラーが出たときのスクリプトはこんな感じ。エラーメッセージからすると、ファイルディスクリプタを開きっぱになってるのかな?

Zip::Archive.open("outdir/test.zip", Zip::CREATE) do |ar|
  1000.times do |count|
    entry = "target.txt" + sprintf("%04d", count)
    ar.add_file(entry, "target.txt")
  end
end


おんで、修正したスクリプトがこちら。ファイルを1つ追加するのに zip を open/close させるように変更してみた。Zip::CREATE オプションを指定しておくと、既存zipがあれば追加モードになってくれるから、期待通りに格納してくれる。

1000.times do |count|
  Zip::Archive.open("outdir/test.zip", Zip::CREATE) do |ar|
    entry = "tkuser.txt" + sprintf("%04d", count)
    ar.add_file(entry, "tkuser.txt")
  end
end


パフォーマンス的にもっと良い解決方法があるかもしれないけど、まずは動くのが最優先だね :-)

環境

  • Mac OSX 10.7.5
  • ruby ruby 2.1.0
  • zipruby (0.3.6)