Class: Philiprehberger::Tar::Reader
- Inherits:
-
Object
- Object
- Philiprehberger::Tar::Reader
- Defined in:
- lib/philiprehberger/tar/reader.rb
Overview
Reads and extracts tar archives.
Constant Summary collapse
- BLOCK_SIZE =
512
Instance Method Summary collapse
-
#each_entry {|Hash| ... } ⇒ Array<Hash>
Iterate over each entry in the archive.
-
#entry?(name) ⇒ Boolean
Check whether the archive contains an entry with the given name, without reading entry content.
-
#initialize(io) ⇒ Reader
constructor
A new instance of Reader.
-
#list ⇒ Array<Hash>
List all entries without reading content.
Constructor Details
#initialize(io) ⇒ Reader
Returns a new instance of Reader.
10 11 12 |
# File 'lib/philiprehberger/tar/reader.rb', line 10 def initialize(io) @io = io end |
Instance Method Details
#each_entry {|Hash| ... } ⇒ Array<Hash>
Iterate over each entry in the archive.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/philiprehberger/tar/reader.rb', line 18 def each_entry(&block) entries = [] loop do header = @io.read(BLOCK_SIZE) break if header.nil? || header.bytesize < BLOCK_SIZE break if header == "\0" * BLOCK_SIZE entry = parse_header(header) break if entry[:name].empty? if entry[:typeflag] == '2' # Symlink entry has no content entry[:content] = '' else content = read_content(entry[:size]) entry[:content] = content end if block block.call(entry) else entries << entry end end entries unless block end |
#entry?(name) ⇒ Boolean
Check whether the archive contains an entry with the given name, without reading entry content.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/philiprehberger/tar/reader.rb', line 52 def entry?(name) loop do header = @io.read(BLOCK_SIZE) break if header.nil? || header.bytesize < BLOCK_SIZE break if header == "\0" * BLOCK_SIZE entry = parse_header(header) break if entry[:name].empty? return true if entry[:name] == name skip_content(entry[:size]) unless entry[:typeflag] == '2' end false end |
#list ⇒ Array<Hash>
List all entries without reading content.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/philiprehberger/tar/reader.rb', line 72 def list result = [] loop do header = @io.read(BLOCK_SIZE) break if header.nil? || header.bytesize < BLOCK_SIZE break if header == "\0" * BLOCK_SIZE entry = parse_header(header) break if entry[:name].empty? result << { name: entry[:name], size: entry[:size], mode: entry[:mode], typeflag: entry[:typeflag], linkname: entry[:linkname] } skip_content(entry[:size]) unless entry[:typeflag] == '2' end result end |