Class: Omnizip::Formats::Rar::Decompressor

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/formats/rar/decompressor.rb

Overview

RAR decompressor wrapper Provides fallback chain: unrar gem → system command → error

Class Method Summary collapse

Class Method Details

.available?Boolean

Check if RAR decompression is available

Returns:

  • (Boolean)

    true if available



15
16
17
# File 'lib/omnizip/formats/rar/decompressor.rb', line 15

def available?
  gem_available? || command_available?
end

.command_available?Boolean

Check if system unrar command is available

Returns:

  • (Boolean)

    true if command available



45
46
47
# File 'lib/omnizip/formats/rar/decompressor.rb', line 45

def command_available?
  !command_path.nil?
end

.command_pathString?

Get unrar command path

Returns:

  • (String, nil)

    Path to unrar or nil



52
53
54
# File 'lib/omnizip/formats/rar/decompressor.rb', line 52

def command_path
  @command_path ||= find_command
end

.extract(archive_path, output_dir, password: nil) ⇒ Object

Extract RAR archive to directory

Parameters:

  • archive_path (String)

    Path to RAR archive

  • output_dir (String)

    Output directory

  • password (String, nil) (defaults to: nil)

    Optional password

Raises:

  • (RuntimeError)

    if extraction fails



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/omnizip/formats/rar/decompressor.rb', line 62

def extract(archive_path, output_dir, password: nil)
  raise "RAR extraction not available" unless available?

  if gem_available?
    extract_with_gem(archive_path, output_dir, password)
  elsif command_available?
    extract_with_command(archive_path, output_dir, password)
  else
    raise unsupported_error
  end
end

.extract_entry(archive_path, entry_name, output_path, password: nil) ⇒ Object

Extract single entry from RAR

Parameters:

  • archive_path (String)

    Path to RAR archive

  • entry_name (String)

    Entry name

  • output_path (String)

    Output path

  • password (String, nil) (defaults to: nil)

    Optional password



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/omnizip/formats/rar/decompressor.rb', line 97

def extract_entry(archive_path, entry_name, output_path,
                  password: nil)
  raise "RAR extraction not available" unless available?

  if gem_available?
    extract_entry_with_gem(archive_path, entry_name,
                           output_path, password)
  elsif command_available?
    extract_entry_with_command(archive_path, entry_name,
                               output_path, password)
  else
    raise unsupported_error
  end
end

.gem_available?Boolean

Check if unrar gem is available

Returns:

  • (Boolean)

    true if gem available



35
36
37
38
39
40
# File 'lib/omnizip/formats/rar/decompressor.rb', line 35

def gem_available?
  require "unrar"
  true
rescue LoadError
  false
end

.infoHash

Get decompressor information

Returns:

  • (Hash)

    Decompressor type and version



22
23
24
25
26
27
28
29
30
# File 'lib/omnizip/formats/rar/decompressor.rb', line 22

def info
  if gem_available?
    { type: :gem, version: gem_version }
  elsif command_available?
    { type: :command, version: command_version }
  else
    { type: :none, version: nil }
  end
end

.list(archive_path) ⇒ Array<Hash>

List RAR archive contents

Parameters:

  • archive_path (String)

    Path to RAR archive

Returns:

  • (Array<Hash>)

    Entry information

Raises:

  • (RuntimeError)

    if listing fails



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/omnizip/formats/rar/decompressor.rb', line 79

def list(archive_path)
  raise "RAR extraction not available" unless available?

  if gem_available?
    list_with_gem(archive_path)
  elsif command_available?
    list_with_command(archive_path)
  else
    raise unsupported_error
  end
end