$ gnpm install gzbz2
gzbz2 - A Node.js interface to streaming gzip/bzip2 compression (built originally from wave.to/node-compress)
supports Buffe or string as both input and output, this is controlled by providing encodings to init (to produce Buffers), or by passing whichever you are using as input (with optional encoding for strings). Bzip and Gzip have the same interfaces, see Versions for specific options info. Also there are two simple js wrappers for producing usable read streams, gunzipstream.js and bunzipstream.js.
To install, ensure that you have libz (and libbz2) installed:
npm install gzbz2
or
waf way: node-waf configure [--no-bzip] [--no-gzip]
gyp way: node-gype configure (no idea how to conditionally configure..)
node-gyp build
Note: on osx, you must run node-waf with python2.6 or greater for configure to work properly. (unsure if this applies to node-gyp)
var gzbz2 = require("gzbz2");
var util = require("util");
// Create gzip stream
var gzip = new gzbz2.Gzip;
// binary string output
// init also accepts level: [0-9]
gzip.init({encoding: 'binary'});
// Pump data to be compressed
var gzdata1 = gzip.deflate("My data that needs ", "ascii");
util.puts("Compressed size : "+gzdata1.length);
// treat string as binary encoded
var gzdata2 = gzip.deflate("to be compressed. 01234567890.");
util.puts("Compressed size : "+gzdata2.length);
var gzdata3 = gzip.end(); // important to capture end data
util.puts("Last bit : "+gzdata3.length);
// Normally stream this out as its generated, but just print here
var gzdata = gzdata1+gzdata2+gzdata3;
util.puts("Total compressed size : "+gzdata.length);
var gzbz2 = require("gzbz2");
var fs = require("fs");
var gunzip = new gzbz2.Gunzip;
gunzip.init({encoding: "utf8"});
var gzdata = fs.readFileSync("somefile.gz", "binary");
var inflated = gunzip.inflate(gzdata, "binary");
gunzip.end(); // returns nothing
var fs = require('fs'),
gunzip = require('gzbz2/gunzipstream');
var stream = gunzip.wrap(process.argv[2], {encoding: process.argv[3]});
stream.on('data', function(data) {
process.stdout.write(data);
});
stream.on('end', function() {
process.exit(0);
});
Copyright 2013 - present © cnpmjs.org | Home |