orlandov / node-compress

A streaming compression for node.js.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

About
=====

Asynchronous streaming compression module for node.js.
Note, that API has changed since forked from original project by waveto. See
HISTORY for more details.
Currently library supports two compression backends: gzip and bzip2.
To install, ensure that you have libz and libbz2 installed.


Build
=====

$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3

$ node-waf configure
$ node-waf build

Configuration options:
  --debug             Build with debug information.
  --with-gzip         Build with gzip support. Default.
  --no-gzip           Build w/o gzip support.
  --with-bzip         Build with bzip support.
  --no-bzip           Build w/o bzip support. Default.

Build puts the compress-bindings.node binary module in build/default. 


Usage examples
==============


var use_buffers = true, gzip_headers = false, compression_level = 9;


var Gzip = require('compress').Gzip,
    Gunzip = require('compress').Gunzip,
    original = new Buffer([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
                           17,18,19,20,21,22,23,24,25,26,27,28.29,30,
                           32,33,34,35,36,37,38,39,40,41,42,
                           0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
                           17,18,19,20,21,22,23,24,25,26,27,28.29,30,
                           32,33,34,35,36,37,38,39,40,41,42]);

function comp_buff(b1,b2) {
  if(b1.length != b2.length) return false;
  for(var i=0; i<b1.length; i++)
    if(b1[i] != b2[i]) return false;
  return true;
}

function test() {
  var   gzip = new Gzip(compression_level, use_buffers, gzip_headers),
      gunzip = new Gunzip(use_buffers, gzip_headers);
  gzip.write(original,
    function(err, d1) {
      gzip.close(
        function(err, d2) {
          var compressed = d1;
          if(d2) {
            compressed = new Buffer(d1.length + d2.length);
            d1.copy(compressed, 0);
            d2.copy(compressed, d1.length);
          }
          if(compressed.length > original.length)
            console.log("Compressed length: " + compressed.length);

          gunzip.write(compressed,
            function(err, i1) {
              gunzip.close(
                function(err, i2) {
                  var uncompressed = i1;
                  if(i2 && i2.length) {
                    uncompressed = new Buffer(i1.legnth + i2.length);
                    i1.copy(uncompressed, 0);
                    i2.copy(uncompressed, i1.length);
                  }
                  if(uncompressed.length != original.length)
                    console.log("Uncompressed length: " + uncompressed.length);
                  if(! comp_buff(original, uncompressed))
                    console.log("Failed");
                });
            });
        });
  });
}

for(i=0; i<100; i++) test();

About

A streaming compression for node.js.

License:MIT License


Languages

Language:C++ 64.9%Language:JavaScript 35.1%