Skip to content

Instantly share code, notes, and snippets.

@coryvirok
Created October 16, 2012 02:53
Show Gist options
  • Save coryvirok/3897000 to your computer and use it in GitHub Desktop.
Save coryvirok/3897000 to your computer and use it in GitHub Desktop.
nodejs plaintext -> zlib compressed data -> base64 and back... why doesn't it work?
var zlib = require('zlib');
var plaintext = 'Hello World';
var compressedData = [];
var compressor = zlib.createGzip();
compressor.write(plaintext);
compressor.end();
compressor.on('data', function(d) { compressedData.push(d);});
compressor.on('end', function() {
var b64CompressedData = new Buffer(compressedData.join('')).toString('base64');
var b64CompressedBuf = new Buffer(b64CompressedData, 'base64');
var decompressedData = [];
var decompressor = zlib.createGunzip();
decompressor.write(b64CompressedBuf);
decompressor.end();
decompressor.on('data', function(d) { decompressedData.push(d); });
decompressor.on('error', function(e) { console.error('error: ' + e); });
decompressor.on('end', function() {
console.log(decompressedData.join(''));
});
});
@coryvirok
Copy link
Author

It apparently works when I use the built-ins:

zlib.gzip(plaintext, function(err, buf) {
  var b64 = new Buffer(buf).toString('base64');

  zlib.gunzip(new Buffer(b64, 'base64'), function(err, buf) {
    console.log(err);
    console.log(buf.toString());
  });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment