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 " in the Gemfile and execute "bundle install"

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 " at the beginning of the method! the header of method looks like the following block:

    def <format> (archive, destination)     require <gem>   ...     end

Instance Method Summary collapse

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.

Parameters:

  • gz_file (String)

    the gz file.

  • destination (String)

    Destination of extracted data.



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 ...)

Parameters:

  • rar_file (String)

    the rar file.

  • destination (String)

    Destination of extracted data.



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.

Parameters:

  • seven_zip_file (String)

    the 7zip file.

  • destination (String)

    Destination of extracted data.



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

Parameters:

  • tar_file (String)

    the tar file.

  • destination (String)

    Destination of extracted data.



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.

Parameters:

  • zip_file (String)

    the zip file.

  • destination (String)

    Destination of extracted data.



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