Class: Archive::Ar::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/archive/ar/reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ Reader

Returns a new instance of Reader.



4
5
6
7
# File 'lib/archive/ar/reader.rb', line 4

def initialize(source, options)
  @source = source 
  @options = options
end

Instance Method Details

#each(full = false, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/archive/ar/reader.rb', line 15

def each(full = false, &block)
  case @source
    when IO
      parse(@source, full); @source.rewind
    else
      File.open(@source, 'r') { |f| parse(f, full) }
  end

  @index.each { |path| yield *(@records[path]) }
end

#extract(dest_dir, options) ⇒ Object



9
10
11
12
13
# File 'lib/archive/ar/reader.rb', line 9

def extract(dest_dir, options)
  each do |header, data|
    Archive::Ar::Format.extract_file(dest_dir, header, data, options)
  end
end

#parse(io, full = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/archive/ar/reader.rb', line 26

def parse(io, full = false)
  @index = []
  @records = {}

  Archive::Ar::Format.read_global_header(io)
  
  until io.eof?
    header = Archive::Ar::Format.read_header(io)
    size = header[:size]
    name = header[:name]

    @index << name
    @records[name] = [header, io.read(size)]

    # Data sections are 2 byte aligned
    if size % 2 == 1
      io.read(1)
    end
  end

  @records
end