跳至内容 跳至搜索

Active Support Gzip

zlib 标准库的便捷封装,允许使用 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!"
命名空间
方法
C
D

类公共方法

compress(source, level = Zlib::DEFAULT_COMPRESSION, strategy = Zlib::DEFAULT_STRATEGY)

使用 gzip 压缩字符串。

# File activesupport/lib/active_support/gzip.rb, line 32
def self.compress(source, level = Zlib::DEFAULT_COMPRESSION, strategy = Zlib::DEFAULT_STRATEGY)
  output = Stream.new
  gz = Zlib::GzipWriter.new(output, level, strategy)
  gz.mtime = 0
  gz.write(source)
  gz.close
  output.string
end

decompress(source)

解压缩 gzipped 字符串。

# File activesupport/lib/active_support/gzip.rb, line 27
def self.decompress(source)
  Zlib::GzipReader.wrap(StringIO.new(source), &:read)
end