简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM