Class: IOP::ZlibCompressor

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

Overview

Filter class to perform data compression with Zlib algorithm.

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

Note that this class does not produce valid .gz files - use GzipCompressor for this purpose.

Use case: compress a string.

require 'iop/zlib'
require 'iop/string'
( IOP::StringSplitter.new('Hello IOP') | IOP::ZlibCompressor.new | (s = IOP::StringMerger.new) ).process!
puts s.to_s

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) ⇒ ZlibCompressor

Creates class instance.

Parameters:

  • args (Array)

    arguments passed to +Zlib::Deflate+ constructor

Since:

  • 0.1



32
33
34
# File 'lib/iop/zlib.rb', line 32

def initialize(*args)
  @args = args
end

Instance Method Details

#process(data = nil) ⇒ Object

Since:

  • 0.1



36
37
38
39
40
41
42
43
# File 'lib/iop/zlib.rb', line 36

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

#process!Object

Since:

  • 0.1



45
46
47
48
49
50
51
52
# File 'lib/iop/zlib.rb', line 45

def process!
  @deflate = Zlib::Deflate.new(*@args)
  begin
    super
  ensure
    @deflate.close
  end
end