Class: IOP::ZstdCompressor

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

Overview

Note:

this class depends on external zstdlib gem.

Filter class to perform data compression with Zstandard algorithm.

This class produces valid .zst files.

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

require 'iop/file'
require 'iop/string'
require 'iop/zstdlib'
( IOP::StringSplitter.new('Hello IOP') | IOP::ZstdCompressor.new(Zstdlib::BEST_COMPRESSION) | IOP::FileWriter.new('hello.zst') ).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) ⇒ ZstdCompressor

Creates class instance.

Parameters:

  • args (Array)

    arguments passed to Zstdlib::Deflate constructor

Since:

  • 0.1



31
32
33
# File 'lib/iop/zstdlib.rb', line 31

def initialize(*args)
  @args = args
end

Instance Method Details

#process(data = nil) ⇒ Object

Since:

  • 0.1



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

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

#process!Object

Since:

  • 0.1



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

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