Gzipping a File Using Zlib in NodeJS


I'm trying to write a simple Cakefile build script for a small javascript library. I'd like to gzip a source string and write the result to a file. Here's what I have so far:

zlib.deflate minifiedjavaScriptSource, (error, buffer) ->
    fs.writeFileSync(javascript_destination_gzipped_minified_path, buffer)

When I run the script, it generated a .gz file as expected. However, when I decompress this file, I get a .cpgz file. If I try and decompress that, it just generates the original file again. What am I doing wrong?

Problem courtesy of: LandonSchropp


You want gzip, but are using deflate instead. :)

try zlib.gzip ..., that works fine:

% coffee
coffee> zlib = require('zlib'); 'ok'
coffee> fs = require('fs'); 'ok'
coffee> zlib.gzip('qweqweqweqwe', (_, buf) -> fs.writeFile('/tmp/test.gz', buf))

% zcat /tmp/test.gz
Solution courtesy of: alex


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.