 <p>I think that might have something to do that you haven't properly closed the files. I took the liberty to rewrite your code, without the chmod stuff (which I don't think is necessary)</p> <pre><code>filename = &lt;your sourcefilename goes here&gt; filename_gz = filename + ".gz" filepath = Rails.root + filename filepath_gz = Rails.root + filename_gz # gzip the file buffer = "" File.open(filepath) do |file| Zlib::GzipWriter.open(filepath_gz) do |gz| while file.read(4096, buffer) gz &lt;&lt; buffer end end end # moves the filepath_gz to filepath (overwriting the original file in the process!) FileUtils.mv(filepath_gz, filepath) </code></pre> <p>As you can see I've used File.open(path) and passed a block. This has the effect that the files will be closed automatically when the block exits. I've also changed the delete/rename code to simply move the gziped file to the original path, which has the same effect.</p> <p>However, I strongly advice you to keep a backup of your original file.</p>