When you want to ZIP download the image data saved locally

What you want to solve

I want to zip download main.jpg and top_map_off.jpg that are saved locally. image.png

solution

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

When you want to ZIP download the image data saved locally
When you want to use the method outside
When you want to change the MySQL password of docker-compose
[Docker] Magic command when you want to wipe out none image
A memorandum when you want to see the data acquired by Jena & SPARQL for each variable.
When you want to bind InputStream in JDBI3
[Ruby] When you want to replace multiple characters
[rails] After option useful when you want to change the order of DB columns
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
When you want to dynamically replace Annotation in Java8
If you want to recreate the instance in cloud9
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
How to write in Model class when you want to save binary data in DB with PlayFramework
When you want to change the wording to be displayed when making a select box from enum
Use JLine when you want to handle keystrokes on the console character by character in Java
When you want to reflect the Master Branch information in the Current Branch you are currently working on
Solution when the image data becomes nil via the confirmation screen
When you want to explicitly write OR or AND with ransack
I want to distinct the duplicated data with has_many through
I want to transition to the same screen in the saved state
docker-compose.yml when you want to keep mysql running with docker
lombok.config when you want to pass @Qualifier to @RequiredArgsConstructor with lombok
Delegate is convenient to use when you want to reuse parts
[Swift] When you want to know if the number of characters in a String matches a certain number ...
Comparison of version strings (Java implementation) when you want to branch the process between two versions
What to do when you want to know the source position where the method is defined in binding.pry
When you want to check whether the contents of a property can be converted to a specific type
ProxyFactory is convenient when you want to test AOP in Spring!
[PostgreSQL] If you want to delete the Rails app, delete the database first!
If you want to include the parent class in Lombok's @builder
If you want to change the Java development environment from Eclipse
When you have introduced devise but want to add more columns
[GCP] Until you push the local Docker image to the Container Registry
[Rails] I want to add data to Params when transitioning with link_to
Practice to use when you want to execute different processing groups serially
When you want to add a string type column with a limited length with the `rails generate migration` command
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
I want to download a file on the Internet using Ruby and save it locally (with caution)
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js
When will you do the refactoring?
[IOS] What to do when the image is filled with one color
When you want Rails to disable a session for a specific controller only
Don't forget to release it when you get the object from S3!
I want to know the JSP of the open portlet when developing Liferay
Summary of means when you want to communicate with HTTP on Android
I want to get the IP address when connecting to Wi-Fi in Java
I want to display an error message when registering in the database
If you want to make a zip file with Ruby, it's rubyzip.
I want you to use Enum # name () for the Key of SharedPreference