Module: Sidetree::Util::Compressor

Included in:
Model::CASFileBase
Defined in:
lib/sidetree/util/compressor.rb

Constant Summary collapse

ESTIMATE_DECOMPRESSION_MULTIPLIER =

The estimated ratio/multiplier of decompressed Sidetree CAS file size compared against the compressed file size.

3

Class Method Summary collapse

Class Method Details

.compress(data) ⇒ String

Compresses teh data in gzip and return it as buffer.

Parameters:

  • data (String)

    Data to be compressed.

Returns:

  • (String)

    compressed data.



14
15
16
17
18
19
20
21
# File 'lib/sidetree/util/compressor.rb', line 14

def compress(data)
  io = StringIO.new("w")
  Zlib::GzipWriter.wrap(io) do |w|
    w.mtime = 0
    w.write data
  end
  io.string.force_encoding("binary")
end

.decompress(compressed, max_bytes: nil) ⇒ String

Decompresses compressed.

Parameters:

  • compressed (String)

    compressed data.

Returns:

  • (String)

    decompressed data.

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/sidetree/util/compressor.rb', line 27

def decompress(compressed, max_bytes: nil)
  if max_bytes && compressed.bytesize > max_bytes
    raise Sidetree::Error, "Exceed maximum compressed chunk file size."
  end
  io = StringIO.new(compressed)
  result = StringIO.new
  Zlib::GzipReader.wrap(io) { |gz| result << gz.read }
  result.string
end