Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created January 15, 2019 15:02
Show Gist options
  • Save miguelmota/837926791cedd3d06e909738c76c4578 to your computer and use it in GitHub Desktop.
Save miguelmota/837926791cedd3d06e909738c76c4578 to your computer and use it in GitHub Desktop.
Node.js gzip compress and decompress example
const zlib = require('zlib')
const buf = Buffer.from('hello world', 'utf-8')
zlib.gzip(buf, (err, compressed) => {
if (err) {
console.error(err)
}
console.log(compressed) // <Buffer 1f 8b 08 00 00 00 00 00 00 13 cb 48 cd c9 c9 57 28 cf 2f ca 49 01 00 85 11 4a 0d 0b 00 00 00>
zlib.unzip(compressed, (err, decompressed) => {
if (err) {
console.error(err)
}
console.log(decompressed.toString()) // "hello world"
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment