Optional cd zip

This is not the right way to do this. Reuse the to_file method.
When generating the writer, i.e., use ether one or other:

writer = File.open(filename, 'w')
writer = Zlib::GzipWriter.open(filename)

then use writer object...