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

稿源:Node.js Recipes (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Gzipping a File Using Zlib in NodeJS

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录