Gzipping a File Using Zlib in NodeJS

Problem

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

Solution

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

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

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

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

Discussion

There is currently no discussion for this recipe.

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