Sha256: be760cf4961a9e57c49cb6c118508548c7b9bb2540ede716a84364457d9baa3c

Contents?: true

Size: 990 Bytes

Versions: 155

Compression:

Stored size: 990 Bytes

Contents

require 'zlib'
require 'stringio'

module ActiveSupport
  # A convenient wrapper for the zlib standard library that allows
  # compression/decompression of strings with gzip.
  #
  #   gzip = ActiveSupport::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"
  #
  #   ActiveSupport::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

155 entries across 149 versions & 16 rubygems

Version Path
activesupport-4.2.11.3 lib/active_support/gzip.rb
activesupport-4.2.11.2 lib/active_support/gzip.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/gzip.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/gzip.rb
activesupport-4.2.11.1 lib/active_support/gzip.rb
activesupport-4.2.11 lib/active_support/gzip.rb
activesupport-4.2.10 lib/active_support/gzip.rb
activesupport-4.2.10.rc1 lib/active_support/gzip.rb
activesupport-4.2.9 lib/active_support/gzip.rb
activesupport-4.2.9.rc2 lib/active_support/gzip.rb
activesupport-4.2.9.rc1 lib/active_support/gzip.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-4.2.8/lib/active_support/gzip.rb
activesupport-4.2.8 lib/active_support/gzip.rb
activesupport-4.2.8.rc1 lib/active_support/gzip.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/gzip.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/gzip.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/gzip.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/gzip.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/gzip.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.1/lib/active_support/gzip.rb