Gzipping a File Using Zlib in NodeJS

综合技术 Node.js Recipes (源链)

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

您可能感兴趣的

Learn Handlebars with Node.js and Help Freddy’s Fi... Our friend Freddy Fishman needs our help. He recently launched an aquaponics business to raise fish  and cultivate underwater plants, and he needs ...
Retrieving various hashes from Redis in Node.js How could one retrieve various hashes from Redis in Node.js through node-redis ? The best way of retrieving various hashes seems to be ...
Unusual node.js behavior Today i saw this code, that was running in node.js environment. (>node.exe test.js) var param = (typeof module !== "undefined" && modul...
Non-blocking code in Node.js not working I'm trying to learn how to create non-blocking applications in node.js but my code doesn't seem to be working. nonblocking.js var ht...
Skycatch: drones, data processing and Node.js Node.js events in 2017 2017-05-24 09:55:08 admin News ...
Node.js Recipes责编内容来自:Node.js Recipes (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Gzipping a File Using Zlib in NodeJS



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

使用声明 | 英豪名录