Sha256: 8af6663f7458a436ef2d9db4bd31cac4a9b477ed5348c904140c616c0eca2f7b

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

require "zlib"

module Sidetree
  module Util
    module Compressor
      # The estimated ratio/multiplier of decompressed Sidetree CAS file size compared against the compressed file size.
      ESTIMATE_DECOMPRESSION_MULTIPLIER = 3

      module_function

      # Compresses teh data in gzip and return it as buffer.
      # @param [String] data Data to be compressed.
      # @return [String] compressed data.
      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

      # Decompresses +compressed+.
      # @param [String] compressed compressed data.
      # @return [String] decompressed data.
      # @raise [Sidetree::Error] raise if data exceeds max_bytes size.
      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sidetree-0.1.5 lib/sidetree/util/compressor.rb
sidetree-0.1.4 lib/sidetree/util/compressor.rb
sidetree-0.1.3 lib/sidetree/util/compressor.rb
sidetree-0.1.2 lib/sidetree/util/compressor.rb