Class: IOStreams::Bzip2::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/io_streams/bzip2/reader.rb

Class Method Summary collapse

Class Method Details

.open(file_name_or_io, **args, &block) ⇒ Object

Read from a Bzip2 file or stream, decompressing the contents as it is read



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/io_streams/bzip2/reader.rb', line 5

def self.open(file_name_or_io, **args, &block)
  begin
    require 'rbzip2' unless defined?(RBzip2)
  rescue LoadError => e
    raise(LoadError, "Please install the 'rbzip2' gem for Bzip2 streaming support. #{e.message}")
  end

  if IOStreams.reader_stream?(file_name_or_io)
    begin
      io = RBzip2.default_adapter::Decompressor.new(file_name_or_io)
      block.call(io)
    ensure
      io.close if io && (io.respond_to?(:closed?) && !io.closed?)
    end
  else
    ::File.open(file_name_or_io, 'rb') do |file|
      io = RBzip2.default_adapter::Decompressor.new(file)
      block.call(io)
    end
  end

end