LoginSignup
0
0

More than 3 years have passed since last update.

ローカルに保存されている画像データをZIPダウンロードしたいとき

Posted at

解決したいこと

ローカルに保存されている、main.jpg、top_map_off.jpgをzipダウンロードしたい。
image.png

解決策

画像はバイナリモードでしか書き出せないので、openするときにバイナリモードを指定する。

Zip::OutputStream.open('example.zip') do |zip|
  default_images = ["main", "top_map_off"]
  default_images.each do |default_image|
    img_path = Rails.root.join(
      "app", "lib",  "output", "download_tpl", @kikaku_date.kikaku_cd, "#{target}", "images", "#{default_image}.jpg 
    ) # ローカルに保存されている画像ファイルのフルパス
    zip.put_next_entry "#{@zip_file_basename}/#{target}/images/#{default_image}.jpg"
    zip.print open(img_path, "rb").read  # 画像をバイナリモードで開く
  end
end

Rubyでバイナリデータを標準出力に出力する

0
0
0

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
0
0