Running GzipWriter on same input produces different output

R

Raymond O'Connor

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:
=> "30888fba6a6fadd5b4006000e05c9d14"
=> "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
 
N

Nobuyoshi Nakada

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
 
B

Ben U.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top