I want to zip download main.jpg and top_map_off.jpg that are saved locally.
Images can only be exported in binary mode, so specify binary mode when opening.
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
) #Full path of locally stored image files
zip.put_next_entry "#{@zip_file_basename}/#{target}/images/#{default_image}.jpg "
zip.print open(img_path, "rb").read #Open image in binary mode
end
end
Output binary data to standard output in Ruby
Recommended Posts