Zlib Core Streams

In notebook:
FrontEndMasters Networking and Streams
Created at:
2017-09-30
Updated:
2017-09-30
Tags:
backend Node JS JavaScript

zlib core streams

* `zlib.createGzip(opts)` - transform stream to compress with gzip
* `zlib.createGunzip(opts)` - transform stream to uncompress with gzip
// used in http servers (deflate) ↴
* `zlib.createDeflate(opts)` - transform stream to compress with deflate
* `zlib.createDeflateRaw(opts)` - transform stream to compress with raw deflate
* `zlib.createInflate(opts)` - transform stream to uncompress with deflate
* `zlib.createInflateRaw(opts)` - transform stream to uncompress with raw deflate
* `zlib.createUnzip(opts)` - transform stream to uncompress gzip and deflate

Use zlib.createGunzip(opts) to unzip on the fly

First, creates a gzipped file from the command line: $ gzip -<hello.txt > hello.txt.gz

  //	****		gunzip.js		****

var createGunzip = require('zlib').createGunzip
var createHash = require('crypto').createHash

// just uses stdin for simplicity
process.stdin
  .pipe(createGunzip())
  // let's also add a hash (this part will have to get all the buffer
  // so not totally streaming)
  .pipe(createHash('sha512', { encoding: 'hex' }))
  .pipe(process.stdout)

runs it with $ node gunzip.js < hello.txt.gz and it prints out the sha of the original file (verifies it with :) $ shasum -a 512 < hello.txt