Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active February 8, 2023 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vyspiansky/ed64119c539efd64ae71e25cdcc4767e to your computer and use it in GitHub Desktop.
Save vyspiansky/ed64119c539efd64ae71e25cdcc4767e to your computer and use it in GitHub Desktop.
Node.js gzip compress & 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"
  });
});

Try it on runkit.com or visit the original gist page.

@elifnurkarakoc
Copy link

Hi,
Can you share your node version? Because I couldn't use zlib package with nodejs 18.14.0 version(latest).
Thank you

@vyspiansky
Copy link
Author

vyspiansky commented Feb 8, 2023

Hey! I've tried this code with node v18.14.0 and v16.19.0. Seems worked as expected.

Screenshot 2023-02-08 at 09 54 52

BTW, I use macOS Monterey.

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