Class: Omnizip::Formats::Rar::Reader
- Inherits:
-
Object
- Object
- Omnizip::Formats::Rar::Reader
- Includes:
- Constants
- Defined in:
- lib/omnizip/formats/rar/reader.rb
Overview
RAR archive reader Provides read-only access to RAR archives (single and multi-volume)
Constant Summary
Constants included from Constants
Constants::ARCHIVE_AUTH_INFO, Constants::ARCHIVE_COMMENT, Constants::ARCHIVE_ENCRYPTED, Constants::ARCHIVE_FIRST_VOLUME, Constants::ARCHIVE_LOCKED, Constants::ARCHIVE_NEW_NAMING, Constants::ARCHIVE_RECOVERY, Constants::ARCHIVE_SOLID, Constants::ARCHIVE_VOLUME, Constants::BLOCK_ARCHIVE, Constants::BLOCK_COMMENT, Constants::BLOCK_ENDARC, Constants::BLOCK_FILE, Constants::BLOCK_LONG, Constants::BLOCK_MARKER, Constants::BLOCK_OLD_AUTH, Constants::BLOCK_OLD_EXTRA, Constants::BLOCK_OLD_RECOVERY, Constants::BLOCK_OLD_SUBBLOCK, Constants::BLOCK_SUBBLOCK, Constants::FILE_COMMENT, Constants::FILE_DIRECTORY, Constants::FILE_ENCRYPTED, Constants::FILE_EXT_TIME, Constants::FILE_LARGE, Constants::FILE_SALT, Constants::FILE_SOLID, Constants::FILE_SPLIT_AFTER, Constants::FILE_SPLIT_BEFORE, Constants::FILE_UNICODE, Constants::FILE_VERSION, Constants::METHOD_BEST, Constants::METHOD_FAST, Constants::METHOD_FASTEST, Constants::METHOD_GOOD, Constants::METHOD_NORMAL, Constants::METHOD_STORE, Constants::OS_BEOS, Constants::OS_MACOS, Constants::OS_MSDOS, Constants::OS_OS2, Constants::OS_UNIX, Constants::OS_WIN32, Constants::RAR4_SIGNATURE, Constants::RAR5_FLAG_CHILD_BLOCKS, Constants::RAR5_FLAG_DATA_AREA, Constants::RAR5_FLAG_DATA_INHERITED, Constants::RAR5_FLAG_EXTRA_AREA, Constants::RAR5_FLAG_IS_DIR, Constants::RAR5_FLAG_MULTI_VOLUME, Constants::RAR5_FLAG_UNKNOWN_BLOCKS, Constants::RAR5_HEADER_ENCRYPTION, Constants::RAR5_HEADER_END, Constants::RAR5_HEADER_FILE, Constants::RAR5_HEADER_MAIN, Constants::RAR5_HEADER_SERVICE, Constants::RAR5_SIGNATURE
Instance Attribute Summary collapse
-
#archive_info ⇒ Object
readonly
Returns the value of attribute archive_info.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#volume_manager ⇒ Object
readonly
Returns the value of attribute volume_manager.
Instance Method Summary collapse
-
#entries ⇒ Array<Models::RarEntry>
All entries, parsing on first use — a Reader is always usable; #open remains for eager loading.
-
#extract_all(output_dir, password: nil) ⇒ Object
Extract all files to directory.
-
#extract_entry(entry_name, output_path, password: nil) ⇒ Object
Extract file to output path.
-
#initialize(file_path) ⇒ Reader
constructor
Initialize reader with file path.
-
#list_files ⇒ Array<Models::RarEntry>
List all files in archive.
-
#multi_volume? ⇒ Boolean
Check if multi-volume archive.
-
#open ⇒ Object
Open and parse RAR archive.
-
#total_volumes ⇒ Integer
Get total number of volumes.
-
#valid? ⇒ Boolean
Check if archive is valid RAR format.
-
#volumes ⇒ Array<String>
Get volumes in multi-volume archive.
Constructor Details
#initialize(file_path) ⇒ Reader
Initialize reader with file path
21 22 23 24 25 26 27 28 29 |
# File 'lib/omnizip/formats/rar/reader.rb', line 21 def initialize(file_path) @file_path = file_path @header = nil @entries = [] @archive_info = Models::RarArchive.new(file_path) @volume_manager = VolumeManager.new(file_path) @use_native = true # Prefer native decompression @opened = false end |
Instance Attribute Details
#archive_info ⇒ Object (readonly)
Returns the value of attribute archive_info.
15 16 17 |
# File 'lib/omnizip/formats/rar/reader.rb', line 15 def archive_info @archive_info end |
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
15 16 17 |
# File 'lib/omnizip/formats/rar/reader.rb', line 15 def file_path @file_path end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
15 16 17 |
# File 'lib/omnizip/formats/rar/reader.rb', line 15 def header @header end |
#volume_manager ⇒ Object (readonly)
Returns the value of attribute volume_manager.
15 16 17 |
# File 'lib/omnizip/formats/rar/reader.rb', line 15 def volume_manager @volume_manager end |
Instance Method Details
#entries ⇒ Array<Models::RarEntry>
All entries, parsing on first use — a Reader is always usable; #open remains for eager loading
46 47 48 49 |
# File 'lib/omnizip/formats/rar/reader.rb', line 46 def entries ensure_open @entries end |
#extract_all(output_dir, password: nil) ⇒ Object
Extract all files to directory
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/omnizip/formats/rar/reader.rb', line 93 def extract_all(output_dir, password: nil) ensure_open FileUtils.mkdir_p(output_dir) # Use decompressor to extract all base_path = @volume_manager.first_volume&.path || @file_path Decompressor.extract(base_path, output_dir, password: password) # Set timestamps for extracted files @entries.each do |entry| next unless entry.mtime output_path = File.join(output_dir, entry.name) next unless File.exist?(output_path) File.utime(entry.mtime, entry.mtime, output_path) end end |
#extract_entry(entry_name, output_path, password: nil) ⇒ Object
Extract file to output path
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/omnizip/formats/rar/reader.rb', line 64 def extract_entry(entry_name, output_path, password: nil) ensure_open entry = @entries.find { |e| e.name == entry_name } raise "Entry not found: #{entry_name}" unless entry # Create directory if needed FileUtils.mkdir_p(File.dirname(output_path)) # Extract file if entry.directory? FileUtils.mkdir_p(output_path) else # Try native decompression first, fall back to external if @use_native && native_decompression_available?(entry) extract_entry_native(entry, output_path) else extract_entry_external(entry_name, output_path, password) end # Set timestamp if available File.utime(entry.mtime, entry.mtime, output_path) if entry.mtime end end |
#list_files ⇒ Array<Models::RarEntry>
List all files in archive
54 55 56 |
# File 'lib/omnizip/formats/rar/reader.rb', line 54 def list_files entries end |
#multi_volume? ⇒ Boolean
Check if multi-volume archive
136 137 138 |
# File 'lib/omnizip/formats/rar/reader.rb', line 136 def multi_volume? @volume_manager.multi_volume? || @header&.is_multi_volume end |
#open ⇒ Object
Open and parse RAR archive
34 35 36 37 38 39 40 |
# File 'lib/omnizip/formats/rar/reader.rb', line 34 def open File.open(@file_path, "rb") do |io| parse_archive(io) end @opened = true self end |
#total_volumes ⇒ Integer
Get total number of volumes
129 130 131 |
# File 'lib/omnizip/formats/rar/reader.rb', line 129 def total_volumes @volume_manager.volume_count end |
#valid? ⇒ Boolean
Check if archive is valid RAR format
115 116 117 |
# File 'lib/omnizip/formats/rar/reader.rb', line 115 def valid? !@header.nil? && @header.valid? end |
#volumes ⇒ Array<String>
Get volumes in multi-volume archive
122 123 124 |
# File 'lib/omnizip/formats/rar/reader.rb', line 122 def volumes @volume_manager.volume_paths end |