Class: Omnizip::Formats::Rar5::Reader

Inherits:
Omnizip::Formats::Rar::RarFormatBase show all
Defined in:
lib/omnizip/formats/rar5/reader.rb

Overview

RAR v5 archive reader

Adapter over the primary Formats::Rar::Reader — the one RAR5 parser verified against real archives and unrar — exposing the legacy entry shape (uncompressed_size, modified_time, ...).

RAR 5 uses variable-length integers (vint) and improved header structure.

Examples:

Reading a RAR5 archive

reader = Rar5::Reader.new
File.open("archive.rar", "rb") do |file|
  entries = reader.read_archive(file)
  entries.each { |entry| puts entry.name }
end

Defined Under Namespace

Classes: Entry, HeaderBlock

Constant Summary collapse

METHOD_SYMBOLS =

RAR5 compression-method byte (cinfo bits 8-10) to the legacy symbol vocabulary

{
  0x30 => :store,
  0x31 => :fastest,
  0x32 => :fast,
  0x33 => :normal,
  0x34 => :good,
  0x35 => :best,
}.freeze

Instance Attribute Summary

Attributes inherited from Omnizip::Formats::Rar::RarFormatBase

#spec, #version

Instance Method Summary collapse

Methods inherited from Omnizip::Formats::Rar::RarFormatBase

#block_type_code, #block_type_name, #compress, #compression_method_code, #compression_method_name, #decompress, #dictionary_size_code, #encryption_algorithm, #supports_feature?, #verify_magic_bytes, #write_archive

Constructor Details

#initializeReader

Initialize a RAR v5 reader



36
37
38
# File 'lib/omnizip/formats/rar5/reader.rb', line 36

def initialize
  super("rar5")
end

Instance Method Details

#read_archive(io) ⇒ Array<Entry>

Read a RAR v5 archive

Parameters:

  • io (IO)

    The input stream

Returns:

  • (Array<Entry>)

    The archive entries

Raises:

  • (FormatError)

    If the archive signature is invalid



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/omnizip/formats/rar5/reader.rb', line 45

def read_archive(io)
  data = io.read
  unless verify_magic_bytes(StringIO.new(data))
    raise FormatError, "Invalid RAR v5 signature"
  end

  Dir.mktmpdir("omnizip_rar5_read") do |tmp|
    archive_path = File.join(tmp, "archive.rar")
    File.binwrite(archive_path, data)

    primary = Rar::Reader.new(archive_path)
    primary.open
    primary.list_files.map { |entry| adapt_entry(entry) }
  end
end