Class: Dearchiver::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/dearchiver/processor.rb

Overview

Author:

  • Juan Pablo Genovese

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • options (Hash) (defaults to: {})

    set of configuration options

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
# File 'lib/dearchiver/processor.rb', line 29

def initialize(options = {})
  @filename = options[:filename]
  raise ArgumentError, "Processor: :filename required!" if @filename.nil? or @filename.empty?

  if options[:archive_type].nil? or options[:archive_type].empty?
    @archive_type = File.extname(@filename) if valid_file_type?
  end
  @archive_type ||= options[: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_typeObject (readonly)

Returns a String the archive type



9
10
11
# File 'lib/dearchiver/processor.rb', line 9

def archive_type
  @archive_type
end

#executed_commandObject (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_outputObject (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

#filenameObject (readonly)

Returns a String with the filename



7
8
9
# File 'lib/dearchiver/processor.rb', line 7

def filename
  @filename
end

#list_of_filesObject (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

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/dearchiver/processor.rb', line 46

def crc_ok?
  result = execute_command(archive_options[@archive_type][:crc_check].gsub("<filename>", filename))
  result.include?(archive_options[@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.

Parameters:

  • destination (String)

    the directory with full path to extracts the files to.

Returns:

  • (Array)

    an array of strings with the extracted file list.

Raises:

  • (ArgumentError)


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_options[@archive_type][:decompress].gsub("<filename>", filename).gsub("<extractdir>", destination))
  result.scan(archive_options[@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