Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

class method

Zlib.gzip

(::String) -> ::String
(::String, level: ::Integer) -> ::String
(::String, strategy: ::Integer) -> ::String
(::String, level: ::Integer, strategy: ::Integer) -> ::String
Zlib.gzip(src, level: nil, strategy: nil) → String

Gzip the given string. Valid values of level are Zlib::NO_COMPRESSION, Zlib::BEST_SPEED, Zlib::BEST_COMPRESSION, Zlib::DEFAULT_COMPRESSION (default), or an integer from 0 to 9.

This method is almost equivalent to the following code:

Example
def gzip(string, level: nil, strategy: nil)
  sio = StringIO.new
  sio.binmode
  gz = Zlib::GzipWriter.new(sio, level, strategy)
  gz.write(string)
  gz.close
  sio.string
end

See also Zlib.gunzip

See also .gunzip .inflate .deflate .zlib_version also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0