Class: Archive::Tar::StreamReader

Inherits:
Reader
  • Object
show all
Defined in:
lib/archive/tar/stream_reader.rb

Constant Summary

Constants included from Archive::Tar

VERSION

Instance Method Summary collapse

Methods inherited from Reader

#[], #build_index, #each, #entry?, #extract, #extract_all, #has_entry?, #index, #read, #stat, #stream

Methods included from Archive::Tar

#join_path, #normalize_path, #solve_path

Constructor Details

#initialize(stream, options = {}) ⇒ StreamReader

Returns a new instance of StreamReader.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/archive/tar/stream_reader.rb', line 26

def initialize(stream, options = {})
  options = {
    block_size: 2 ** 19,
    reload_time: 32
  }.merge(options)
  
  stream = IO.new(stream) if io.is_a? Integer

  if options[:compression] == :auto
    raise "Automatic compression is not available for streams"
  end
  
  tmp_file = File.new("/tmp/" + rand(500000).to_s(16) + ".tar", "w+b")
  Thread.new do
    i = 0
  
    until stream.eof?
      read = stream.read(options[:block_size])
      tmp_file.write(read)
      self.build_index if i % options[:reload_time] == 0
      i += 1
    end
    
    self.build_index
  end
  
  super(tmp_file, options)
end