Sha256: bca871e41088996590335cdabaa8e1a081e5a5c8614e39211a9d3f26169a0d0c
Contents?: true
Size: 971 Bytes
Versions: 2
Compression:
Stored size: 971 Bytes
Contents
require 'zlib' require 'stringio' module CoreExt # A convenient wrapper for the zlib standard library that allows # compression/decompression of strings with gzip. # # gzip = CoreExt::Gzip.compress('compress me!') # # => "\x1F\x8B\b\x00o\x8D\xCDO\x00\x03K\xCE\xCF-(J-.V\xC8MU\x04\x00R>n\x83\f\x00\x00\x00" # # CoreExt::Gzip.decompress(gzip) # # => "compress me!" module Gzip class Stream < StringIO def initialize(*) super set_encoding "BINARY" end def close; rewind; end end # Decompresses a gzipped string. def self.decompress(source) Zlib::GzipReader.new(StringIO.new(source)).read end # Compresses a string using gzip. def self.compress(source, level=Zlib::DEFAULT_COMPRESSION, strategy=Zlib::DEFAULT_STRATEGY) output = Stream.new gz = Zlib::GzipWriter.new(output, level, strategy) gz.write(source) gz.close output.string end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
core_ext-0.0.6 | lib/core_ext/gzip.rb |
core_ext-0.0.5 | lib/core_ext/gzip.rb |