123456789_123456789_123456789_123456789_123456789_

Class: Zlib::GzipWriter

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, GzipFile
Instance Chain:
self, GzipFile
Inherits: Zlib::GzipFile
Defined in: ext/zlib/zlib.c,
ext/zlib/zlib.c

Overview

GzipWriter is a class for writing gzipped files. GzipWriter should be used with an instance of IO, or IO-like, object.

Following two example generate the same result.

Zlib::GzipWriter.open('hoge.gz') do |gz|
  gz.write 'jugemu jugemu gokou no surikire...'
end

File.open('hoge.gz', 'w') do |f|
  gz = Zlib::GzipWriter.new(f)
  gz.write 'jugemu jugemu gokou no surikire...'
  gz.close
end

To make like gzip(1) does, run following:

orig = 'hoge.txt'
Zlib::GzipWriter.open('hoge.gz') do |gz|
  gz.mtime = File.mtime(orig)
  gz.orig_name = orig
  gz.write IO.binread(orig)
end

NOTE: Due to the limitation of Ruby's finalizer, you must explicitly close GzipWriter objects by Zlib::GzipWriter#close etc. Otherwise, GzipWriter will be not able to write the gzip footer and will generate a broken gzip file.

Class Method Summary

GzipFile - Inherited

.wrap

Creates a GzipReader or GzipWriter associated with io, passing in any necessary extra options, and executes the block with the newly created object just like File.open.

Instance Attribute Summary

GzipFile - Inherited

#sync

Same as IO#sync

#sync=

Same as IO.

#closed?

Same as IO#closed?

Instance Method Summary

GzipFile - Inherited

#close

Closes the GzipFile object.

#comment

Returns comments recorded in the gzip file header, or nil if the comments is not present.

#crc

Returns CRC value of the uncompressed data.

#finish

Closes the GzipFile object.

#level

Returns compression level.

#mtime

Returns last modification time recorded in the gzip file header.

#orig_name

Returns original filename recorded in the gzip file header, or nil if original filename is not present.

#os_code

Returns OS code number recorded in the gzip file header.

#to_io

Same as IO.

Constructor Details

.new(io, level = nil, strategy = nil, options = {})

Creates a GzipWriter object associated with io. level and strategy should be the same as the arguments of Deflate.new. The GzipWriter object writes gzipped data to io. io must respond to the #write method that behaves the same as IO#write.

The options hash may be used to set the encoding of the data. :external_encoding, :internal_encoding and :encoding may be set as in IO.new.

Class Method Details

.open(filename, level = nil, strategy = nil) {|gz| ... }

Opens a file specified by filename for writing gzip compressed data, and returns a GzipWriter object associated with that file. Further details of this method are found in .new and GzipFile.wrap.

Instance Attribute Details

#comment=(str) (writeonly)

Specify the comment (str) in the gzip header.

#mtime=(mtime) (writeonly)

Specify the modification time (mtime) in the gzip header. Using an Integer.

Setting the mtime in the gzip header does not effect the mtime of the file generated. Different utilities that expand the gzipped files may use the mtime header. For example the gunzip utility can use the -N flag which will set the resultant file's mtime to the value in the header. By default many tools will set the mtime of the expanded file to the mtime of the gzipped file, not the mtime in the header.

If you do not set an mtime, the default value will be the time when compression started. Setting a value of 0 indicates no time stamp is available.

#orig_name=(str) (writeonly)

Specify the original name (str) in the gzip header.

Instance Method Details

#<<

#flush(flush = nil)

Flushes all the internal buffers of the GzipWriter object. The meaning of flush is same as in Deflate#deflate. SYNC_FLUSH is used if flush is omitted. It is no use giving flush NO_FLUSH.

#pos Also known as: #tell

Total number of input bytes read so far.

#printf

Same as IO.

#putc(ch)

Same as IO.

#puts

Same as IO.

#tell

Alias for #pos.

#write(str)

Same as IO.