Sha256: cd282cd0eb4ffac7c9db6c096d89448c55115f7c15b4b00d16367ca7cbea8525

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'ffi'

require 'lzo/raw'
require 'lzo/ffi'
require 'lzo/lzop'
require 'lzo/version'
require 'lzo/multi_io'

module LZO
  class << self
    def compress(input, lzop: false)
      io = input.respond_to?(:read) ? input : StringIO.new(input)

      if lzop
        output_io = StringIO.new ''.force_encoding 'BINARY'
        compressor = LzopCompressor.new output_io, name: '', mode: 0100644, mtime: Time.now

        until io.eof?
          bytes = io.read 2**20 # 1MB chunks
          compressor.write bytes
        end

        compressor.close
        output_io.string
      else
        Raw.compress io
      end
    end

    def decompress(input)
      io = input.respond_to?(:read) ? input : StringIO.new(input)

      magic = LZOP::Header::MAGIC
      start = io.read magic.length
      multi_io = MultiIO.new [StringIO.new(start), io]

      if start == magic
        LzopDecompressor.new(multi_io).read
      else
        Raw.decompress multi_io
      end
    end

    def library_version
      FFI.lzo_version
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lzo-0.1.0 lib/lzo.rb