This blog will guide you on how to create a Ruby on Rails application and add rubyzip which used to make zip files with Password Protection.
Let’s have a brief introduction about Password Protection using rubyzip. If you are familiar with rubyzip and Password Protection then directly go to Implementation section.
What is rubyzip ?
- It’s a ruby gem (library) which is used to read and write zip files.
- rubyzip provide you the power of reading and writing zip files and also make password protected zip files.
Why rubyzip?
- rubyzip provides traditional password protection.
Requirements
- Ruby 1.9.2 or greater
Technology Stack
- Technology: Ruby on Rails
- Versions: ruby 2.5.1p57 & rails 5.2.4
- Git Repository: https://github.com/jigarshah8055/zipper
Implementation
-
Create a new rails app
rails new zipper --database=postgresql
-
Now put gem in the gemfile as
gem 'rubyzip', '>= 1.0.0'
Don’t forget to bundle ;)
-
Generate a controller with the create and new method
rails g controller zipping_folders new
-
Simply set the root path to zipping_folders new method and make resource path for only create method in routes.rb
resources :zipping_folders, only: [:create]
root ‘zipping_folders#new’
-
Now make a form in
new.html.erb
with one file tag to choose files and two input fields which define zip file name and password respectively.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters<h1>Welcome to Create Password Protected Zip</h1> <%= form_tag zipping_folders_path, method: :post, multipart: true do %> <%= label_tag 'Content', 'Select Files' %> <%= file_field_tag('files[]', multiple: true, class: 'drop-target') %> <%= label_tag 'Zip Name', 'Zip Name' %> <%= text_field_tag 'out_name' %> <%= label_tag 'Password', 'Password' %> <%= password_field_tag 'password' %> <%= submit_tag 'Generate!' %> <% end %> -
You have to write below code in
zipping_folders_controller.rb
.This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersrequire 'rubygems' require 'zip' class ZippingFoldersController < ApplicationController def new; end def create if params[:files].present? get_variables_for_zip(params) buffer = Zip::OutputStream.write_buffer(::StringIO.new(''), Zip::TraditionalEncrypter.new(@password)) do |out| @input_filenames.each do |filename| out.put_next_entry(filename) out.write File.open("#{@folder}/#{filename}").read end end File.open(@zipfile_name, 'wb') {|f| f.write(buffer.string) } buffer.rewind send_data buffer.read, filename: "#{@zipfile_name}.zip" end end private def get_variables_for_zip(params) @input_filenames = [] params[:files].select {|f| @input_filenames << f.path.gsub('/tmp/', '') } @folder = "/tmp/" @zipfile_name = params[:out_name] @password = params[:password] end end
Bingooo!!! That's it..!!!
- Start your rails server and check it on localhost.
Top comments (0)