Class: IOStreams::Bzip2::Writer

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

Class Method Summary collapse

Class Method Details

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

Write to a file / stream, compressing with Bzip2



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/writer.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.writer_stream?(file_name_or_io)
    begin
      io = RBzip2.default_adapter::Compressor.new(file_name_or_io)
      block.call(io)
    ensure
      io.close
    end
  else
    IOStreams::File::Writer.open(file_name_or_io) do |file|
      io = RBzip2.default_adapter::Compressor.new(file)
      block.call(io)
      io.close
    end
  end
end