Class: IOP::GzipCompressor

Inherits:
Object
  • Object
show all
Includes:
Feed, Sink
Defined in:
lib/iop/zlib.rb

Overview

Filter class to perform Gzip data compression.

This class is an adapter for the standard Ruby Zlib::GzipWriter class.

This class produces valid .gz files.

### Use case: compress a string and store it to .gz file.

require 'iop/zlib'
require 'iop/file'
require 'iop/string'
( IOP::StringSplitter.new('Hello IOP') | IOP::GzipCompressor.new | IOP::FileWriter.new('hello.gz') ).process!

Since:

  • 0.1

Instance Attribute Summary

Attributes included from Sink

#upstream

Attributes included from Feed

#downstream

Instance Method Summary collapse

Methods included from Feed

#|

Constructor Details

#initialize(*args) ⇒ GzipCompressor

Creates class instance.

Parameters:

  • args (Array)

    arguments passed to Zlib::GzipWriter constructor

Since:

  • 0.1



129
130
131
# File 'lib/iop/zlib.rb', line 129

def initialize(*args)
  @args = args
end

Instance Method Details

#process(data = nil) ⇒ Object

Since:

  • 0.1



133
134
135
136
137
138
139
140
# File 'lib/iop/zlib.rb', line 133

def process(data = nil)
  if data.nil?
    @compressor.finish
    super
  else
    @compressor.write(data)
  end
end

#process!Object

Since:

  • 0.1



146
147
148
149
150
151
# File 'lib/iop/zlib.rb', line 146

def process!
  @compressor = Zlib::GzipWriter.new(self, *@args)
  super
ensure
  @compressor.close unless @compressor.nil?
end

#write(data) ⇒ Object

Since:

  • 0.1



142
143
144
# File 'lib/iop/zlib.rb', line 142

def write(data)
  downstream&.process(data)
end