Skip to content

class Compress::Gzip::Writer
inherits IO

A write-only IO object to compress data in the gzip format.

Instances of this class wrap another IO object. When you write to this instance, it compresses the data and writes it to the underlying IO.

Note

unless created with a block, close must be invoked after all data has been written to a Gzip::Writer instance.

Example: compress a file

require "compress/gzip"

File.write("file.txt", "abc")

File.open("./file.txt", "r") do |input_file|
  File.open("./file.gzip", "w") do |output_file|
    Compress::Gzip::Writer.open(output_file) do |gzip|
      IO.copy(input_file, gzip)
    end
  end
end

Class methods

.open(io : IO, level = Compress::Gzip::DEFAULT_COMPRESSION, sync_close = false

Creates a new writer to the given io, yields it to the given block, and closes it at the end.

View source

.open(filename : String, level = Compress::Gzip::DEFAULT_COMPRESSION

Creates a new writer to the given filename, yields it to the given block, and closes it at the end.

View source

.new(io : IO, level = Compress::Gzip::DEFAULT_COMPRESSION, sync_close = false)

Creates a new writer to the given io.

View source

.new(filename : String, level = Compress::Gzip::DEFAULT_COMPRESSION)

Creates a new writer to the given filename.

View source

Methods

#close

Closes this writer. Must be invoked after all data has been written.

View source

#closed? : Bool

Returns true if this writer is closed.

View source

#flush

Flushes data, forcing writing the gzip header if no data has been written yet.

See IO#flush.

View source

#header : Compress::Gzip::Header

The header to write to the gzip stream. It will be written just before the first write to this writer. Changes to the header after the first write are ignored.

View source

#read(slice : Bytes)

Always raises IO::Error because this is a write-only IO.

View source

#sync_close=(sync_close)

Whether to close the enclosed IO when closing this writer.

View source

#sync_close? : Bool

Whether to close the enclosed IO when closing this writer.

View source

#write(slice : Bytes) : Nil

See IO#write.

View source