Class: Omnizip::Formats::Cpio::Reader
- Inherits:
-
Object
- Object
- Omnizip::Formats::Cpio::Reader
- Includes:
- Constants
- Defined in:
- lib/omnizip/formats/cpio/reader.rb
Overview
CPIO archive reader
Reads CPIO archives in newc, CRC, ODC, and binary formats. Automatically detects format from magic number.
Constant Summary
Constants included from Constants
Constants::MAGIC_BINARY, Constants::MAGIC_CRC, Constants::MAGIC_NEWC, Constants::MAGIC_ODC, Constants::NEWC_ALIGNMENT, Constants::NEWC_HEADER_SIZE, Constants::S_IFBLK, Constants::S_IFCHR, Constants::S_IFDIR, Constants::S_IFIFO, Constants::S_IFLNK, Constants::S_IFMT, Constants::S_IFREG, Constants::S_IFSOCK, Constants::S_IRGRP, Constants::S_IROTH, Constants::S_IRUSR, Constants::S_IRWXG, Constants::S_IRWXO, Constants::S_IRWXU, Constants::S_ISGID, Constants::S_ISUID, Constants::S_ISVTX, Constants::S_IWGRP, Constants::S_IWOTH, Constants::S_IWUSR, Constants::S_IXGRP, Constants::S_IXOTH, Constants::S_IXUSR, Constants::TRAILER_NAME
Instance Attribute Summary collapse
-
#entries ⇒ Array<Entry>
readonly
Parsed entries.
-
#file_path ⇒ String
readonly
Archive file path.
-
#format ⇒ Symbol?
readonly
Detected format.
Instance Method Summary collapse
-
#extract_all(output_dir) ⇒ Object
Extract all entries to directory.
-
#extract_entry(entry_name, output_path) ⇒ Object
Extract entry to output path.
-
#format_name ⇒ String
Get archive format.
-
#initialize(file_path) ⇒ Reader
constructor
Initialize CPIO reader.
-
#list ⇒ Array<Entry>
List all entries.
-
#open ⇒ Object
Open and parse CPIO archive.
Constructor Details
#initialize(file_path) ⇒ Reader
Initialize CPIO reader
37 38 39 40 41 |
# File 'lib/omnizip/formats/cpio/reader.rb', line 37 def initialize(file_path) @file_path = file_path @entries = [] @format = nil end |
Instance Attribute Details
#entries ⇒ Array<Entry> (readonly)
Returns Parsed entries.
29 30 31 |
# File 'lib/omnizip/formats/cpio/reader.rb', line 29 def entries @entries end |
#file_path ⇒ String (readonly)
Returns Archive file path.
26 27 28 |
# File 'lib/omnizip/formats/cpio/reader.rb', line 26 def file_path @file_path end |
#format ⇒ Symbol? (readonly)
Returns Detected format.
32 33 34 |
# File 'lib/omnizip/formats/cpio/reader.rb', line 32 def format @format end |
Instance Method Details
#extract_all(output_dir) ⇒ Object
Extract all entries to directory
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/omnizip/formats/cpio/reader.rb', line 75 def extract_all(output_dir) FileUtils.mkdir_p(output_dir) @entries.each do |entry| next if entry.trailer? output_path = File.join(output_dir, entry.name) extract_single_entry(entry, output_path) end end |
#extract_entry(entry_name, output_path) ⇒ Object
Extract entry to output path
65 66 67 68 69 70 |
# File 'lib/omnizip/formats/cpio/reader.rb', line 65 def extract_entry(entry_name, output_path) entry = @entries.find { |e| e.name == entry_name } raise "Entry not found: #{entry_name}" unless entry extract_single_entry(entry, output_path) end |
#format_name ⇒ String
Get archive format
89 90 91 92 93 94 95 96 97 |
# File 'lib/omnizip/formats/cpio/reader.rb', line 89 def format_name case @format when :newc then "CPIO newc (SVR4)" when :crc then "CPIO newc with CRC" when :odc then "CPIO ODC (portable)" when :binary then "CPIO binary (old)" else "Unknown" end end |
#list ⇒ Array<Entry>
List all entries
56 57 58 |
# File 'lib/omnizip/formats/cpio/reader.rb', line 56 def list @entries.reject(&:trailer?) end |
#open ⇒ Object
Open and parse CPIO archive
46 47 48 49 50 51 |
# File 'lib/omnizip/formats/cpio/reader.rb', line 46 def open File.open(@file_path, "rb") do |io| parse_archive(io) end self end |