def write_to(filename, options = {})
options[:compress] ||= File.extname(filename) == '.gz'
if options[:compress]
pathname.open('rb') do |rd|
File.open("#{filename}+", 'wb') do |wr|
gz = Zlib::GzipWriter.new(wr, Zlib::BEST_COMPRESSION)
buf = ""
while rd.read(16384, buf)
gz.write(buf)
end
gz.close
end
end
else
FileUtils.cp(pathname, "#{filename}+")
end
FileUtils.mv("#{filename}+", filename)
File.utime(mtime, mtime, filename)
nil
ensure
FileUtils.rm("#{filename}+") if File.exist?("#{filename}+")
end