Module: Reviser::Components::Extractors
- Included in:
- Archiver
- Defined in:
- lib/reviser/components/extractors.rb
Overview
The module contains all methods to extract an archive regardless the format.
Convention over configuration !
To add a new format, maybe you need to install the a gem.
Find your gem which uncompress a specified format on rubygems.org.
Add the line "gem
Now, you can write the method corresponding to the format.
The name of the method corresponds to the format.
For example, if you want to use 'rar' format, the name of the method will be: "rar"
Don't forget to require the gem: "require
def <format> (archive, destination) require <gem> ... end
Instance Method Summary collapse
-
#gz(gz_file, destination) ⇒ Object
(also: #tgz)
Method which ungz a file.
-
#method_missing(m, *args, &block) ⇒ Object
Use first of all for seven zip format (little tip, can't call it directly).
-
#rar(rar_file, destination) ⇒ Object
Method which unrar a rar file, if it is possible (proprietary format grr ...).
-
#seven_zip(seven_zip_file, destination) ⇒ Object
Method which un7zip a 7zip file.
- #tar(tar_file, destination) ⇒ Object
-
#zip(zip_file, destination) ⇒ Object
Method which unzips a file.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
Use first of all for seven zip format (little tip, can't call it directly).
152 153 154 155 156 157 158 |
# File 'lib/reviser/components/extractors.rb', line 152 def method_missing(m, *args, &block) if (ext = File.extname(args[0]).delete('.') == '7z') seven_zip(args[0], args[1]) else raise "Format '#{ext.delete('.')}' not supported" end end |
Instance Method Details
#gz(gz_file, destination) ⇒ Object Also known as: tgz
Method which ungz a file.
80 81 82 83 84 85 86 |
# File 'lib/reviser/components/extractors.rb', line 80 def gz gz_file, destination require 'zlib' file = Zlib::GzipReader.open(gz_file) unzipped = StringIO.new(file.read) file.close tar(unzipped, destination) end |
#rar(rar_file, destination) ⇒ Object
Method which unrar a rar file, if it is possible (proprietary format grr ...)
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/reviser/components/extractors.rb', line 126 def rar rar_file, destination require 'shellwords' `which unrar` if $?.success? src = Shellwords.escape(rar_file) destination = Shellwords.escape(destination) `unrar e #{src} #{destination}` else raise 'Please install unrar : sudo apt-get install unrar (winRar for Windows)' end end |
#seven_zip(seven_zip_file, destination) ⇒ Object
Method which un7zip a 7zip file.
141 142 143 144 145 146 147 148 149 |
# File 'lib/reviser/components/extractors.rb', line 141 def seven_zip seven_zip_file, destination require_gem 'seven_zip' unless defined? SevenZipRuby File.open(seven_zip_file, 'rb') do |file| SevenZipRuby::Reader.open(file) do |szr| szr.extract_all destination end end end |
#tar(tar_file, destination) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/reviser/components/extractors.rb', line 95 def tar tar_file, destination require 'rubygems/package' # test if src is String (filename) or IO stream if tar_file.is_a? String stream = File.open(tar_file) else stream = tar_file end Gem::Package::TarReader.new(stream) do |tar| tar.each do |tarfile| destination_file = File.join destination, tarfile.full_name if tarfile.directory? FileUtils.mkdir_p destination_file else destination_directory = File.dirname(destination_file) FileUtils.mkdir_p destination_directory unless File.directory?(destination_directory) File.open destination_file, 'wb' do |f| f.print tarfile.read end end tarfile.close unless tarfile.closed? end tar.close end stream.close end |
#zip(zip_file, destination) ⇒ Object
Method which unzips a file.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/reviser/components/extractors.rb', line 55 def zip zip_file, destination require 'zip' # Cfg the gem Zip.on_exists_proc = true Zip.continue_on_exists_proc = true Zip::File.open(zip_file) do |archive| #Entry = file or directory archive.each do |entry| #Create filepath filepath = File.join(destination, entry.name) # Check if it doesn't exist because of directories (overwrite) unless File.exist?(filepath) # Create directories to access file FileUtils.mkdir_p(File.dirname(filepath)) entry.extract(filepath) end end end end |