Class: Omnizip::ArchiveHandlers::RarHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/archive_handlers/rar_handler.rb

Overview

Adapter for the RAR format: extraction/listing/reading, plus creation through Formats::Rar (RAR5 STORE is unrar-verified; compressed methods fall back to STORE with a warning — see the README interop table).

Defined Under Namespace

Classes: WriterProxy

Instance Method Summary collapse

Instance Method Details

#create(path, **options, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/omnizip/archive_handlers/rar_handler.rb', line 13

def create(path, **options, &block)
  proxy = nil
  result = Omnizip::Formats::Rar.create(path, options) do |writer|
    proxy = WriterProxy.new(writer)
    block&.call(proxy)
  end
  proxy&.cleanup
  result
end

#extract_to(path, output_dir, password: nil, **_) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/omnizip/archive_handlers/rar_handler.rb', line 23

def extract_to(path, output_dir, password: nil, **_)
  Omnizip::Formats::Rar::Decompressor.extract(path, output_dir,
                                              password: password)
  reader = Omnizip::Formats::Rar::Reader.new(path).open
  reader.list_files.reject(&:is_dir)
    .map { |e| ::File.join(output_dir, e.name) }
end

#list(path, details: false, **_) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/omnizip/archive_handlers/rar_handler.rb', line 31

def list(path, details: false, **_)
  reader = Omnizip::Formats::Rar::Reader.new(path).open
  entries = reader.list_files
  if details
    entries.map do |e|
      { name: e.name, size: e.size, directory: e.is_dir,
        compressed_size: (e.compressed_size if e.compressed_size.positive?),
        mtime: e.mtime }
    end
  else
    entries.map(&:name)
  end
end

#read_entry(path, entry_name, password: nil, **_) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/omnizip/archive_handlers/rar_handler.rb', line 45

def read_entry(path, entry_name, password: nil, **_)
  Dir.mktmpdir("omnizip-rar-entry") do |dir|
    dest = File.join(dir, "entry")
    Omnizip::Formats::Rar::Decompressor.extract_entry(
      path, entry_name, dest, password: password
    )
    File.binread(dest)
  end
end