Running GzipWriter on same input produces different output

Hi,

Is it normal that everytime I gzip the same contents, the output is
different?
For example, calling gzip writer on the same input twice, gives
different results:

strio = StringIO.open(’’,‘w’)
gz = Zlib::GzipWriter.new(strio)
gz.write(‘foobar’)
gz.close
Digest::MD5.hexdigest(strio.string)
=> “30888fba6a6fadd5b4006000e05c9d14”

strio = StringIO.open(’’,‘w’)
gz = Zlib::GzipWriter.new(strio)
gz.write(‘foobar’)
gz.close
Digest::MD5.hexdigest(strio.string)
=> “e09426945afb531b3db49733060b8d08”

I’m trying to compare 1 file that is unzipped versus another file that
is gzipped on a network store (to determine whether to update it or
not). So I gzip the unzipped file, generate an md5 hash for it, and
then compare the md5 hash to the md5 of the gzip file on the server.
However, I soon noticed that calling gzipwriter on the same file
contents over and over produced a different md5 hash everytime. Oddly
when I use the gzip system command I don’t get this behavior. The gzip
data it produces is the same each time.

Any help would be appreciated.
Thanks,
Ray

Hi,

At Sat, 1 Dec 2007 08:46:09 +0900,
Raymond O’Connor wrote in [ruby-talk:281540]:

Is it normal that everytime I gzip the same contents, the output is
different?

Yes.

$ ruby /usr/bin/irb -rstringio -rzlib --prompt xmp
strio = StringIO.new("")
==>#StringIO:0x7ff087e4
gz = Zlib::GzipWriter.new(strio)
==>#Zlib::GzipWriter:0x7fef390c
t = Time.now; gz.write(“foobar”)
==>6
gz.close
==>#StringIO:0x7ff087e4
[t.to_i, strio.string.unpack(“x4V”)[0]]
==>[1196476556, 1196476556]
_[0] == _[1]
==>true

I think the reason for this is because gzip saves the original filename
and timestamp. If no timestamp is present gzip uses the gzip file
creation time.

If you want the files to be the same, as I did, you can set the
modification time when using GzipWriter. I.e.:

require ‘zlib’

Zlib::GzipWriter.open(’/tmp/file.gz’) do |gz|
gz.mtime = 1
gz.write “contents”
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs