Type:
Class

Zlib::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.

1
2
3
4
5
6
7
8
9
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:

1
2
3
4
5
6
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::GzipFile#close etc. Otherwise, GzipWriter will be not able to write the gzip footer and will generate a broken gzip file.

mtime=
  • References/Ruby on Rails/Ruby/Classes/Zlib/Zlib::GzipWriter

mtime=(p1) Instance Public methods Specify the modification time (mtime)

2025-01-10 15:47:30
open
  • References/Ruby on Rails/Ruby/Classes/Zlib/Zlib::GzipWriter

Zlib::GzipWriter.open(filename, level=nil, strategy=nil) { |gz| ... } Class Public methods

2025-01-10 15:47:30
comment=
  • References/Ruby on Rails/Ruby/Classes/Zlib/Zlib::GzipWriter

comment=(p1) Instance Public methods Specify the comment (str)

2025-01-10 15:47:30
printf
  • References/Ruby on Rails/Ruby/Classes/Zlib/Zlib::GzipWriter

printf(*args) Instance Public methods Same as

2025-01-10 15:47:30