Class: Dearchiver::Processor
- Inherits:
-
Object
- Object
- Dearchiver::Processor
- Defined in:
- lib/dearchiver/processor.rb
Overview
Instance Attribute Summary collapse
-
#archive_type ⇒ Object
readonly
Returns a String the archive type.
-
#executed_command ⇒ Object
readonly
Returns a String with the command executed.
-
#execution_output ⇒ Object
readonly
Returns a String with the result of the executed command.
-
#filename ⇒ Object
readonly
Returns a String with the filename.
-
#list_of_files ⇒ Object
readonly
Returns an Array of strings with the list of extracted files.
Instance Method Summary collapse
-
#crc_ok? ⇒ Boolean
Checks the CRC of the file.
-
#extract_to(destination) ⇒ Array
Extracts the content of the compressed file to the specified directory.
-
#initialize(options = {}) ⇒ Dearchiver::Processor
constructor
Initialize a new Dearchiver::Processor object.
Constructor Details
#initialize(options = {}) ⇒ Dearchiver::Processor
Initialize a new Dearchiver::Processor object
Possible values for options
:filename - The complete filename (with included path) to work with.
:archive_type - Optional. See archive_options for recognized file types.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dearchiver/processor.rb', line 29 def initialize( = {}) @filename = [:filename] raise ArgumentError, "Processor: :filename required!" if @filename.nil? or @filename.empty? if [:archive_type].nil? or [:archive_type].empty? @archive_type = File.extname(@filename) if valid_file_type? end @archive_type ||= [:archive_type] raise ArgumentError, "Processor: :archive_type required. :filename does not contain a recognizable extension!" if @archive_type.nil? or @archive_type.empty? end |
Instance Attribute Details
#archive_type ⇒ Object (readonly)
Returns a String the archive type
9 10 11 |
# File 'lib/dearchiver/processor.rb', line 9 def archive_type @archive_type end |
#executed_command ⇒ Object (readonly)
Returns a String with the command executed
13 14 15 |
# File 'lib/dearchiver/processor.rb', line 13 def executed_command @executed_command end |
#execution_output ⇒ Object (readonly)
Returns a String with the result of the executed command
15 16 17 |
# File 'lib/dearchiver/processor.rb', line 15 def execution_output @execution_output end |
#filename ⇒ Object (readonly)
Returns a String with the filename
7 8 9 |
# File 'lib/dearchiver/processor.rb', line 7 def filename @filename end |
#list_of_files ⇒ Object (readonly)
Returns an Array of strings with the list of extracted files
11 12 13 |
# File 'lib/dearchiver/processor.rb', line 11 def list_of_files @list_of_files end |
Instance Method Details
#crc_ok? ⇒ Boolean
Checks the CRC of the file
Returns true if the CRC is Ok, false otherwise
46 47 48 49 |
# File 'lib/dearchiver/processor.rb', line 46 def crc_ok? result = execute_command([@archive_type][:crc_check].gsub("<filename>", filename)) result.include?([@archive_type][:crc_ok]) ? true : false end |
#extract_to(destination) ⇒ Array
Extracts the content of the compressed file to the specified directory. Warning: it will overwrite the existing files without asking.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dearchiver/processor.rb', line 59 def extract_to(destination) raise ArgumentError, "Processor: destination is required!" if destination.nil? or destination.empty? raise RuntimeError, "destination directory is not valid" unless Dir.exists?(destination) @list_of_files = [] result = execute_command([@archive_type][:decompress].gsub("<filename>", filename).gsub("<extractdir>", destination)) result.scan([@archive_type][:file_list_regex]).each do |slice| # The gsub("\b","") is a hack to make the list file for unrar work. @list_of_files << slice.first.gsub("\b","").strip end @list_of_files end |