Skip to content

Instantly share code, notes, and snippets.

@zakcodez
Created November 5, 2021 09:55
Show Gist options
  • Save zakcodez/af07a68823dfc99f77d2f11ce6cce26a to your computer and use it in GitHub Desktop.
Save zakcodez/af07a68823dfc99f77d2f11ce6cce26a to your computer and use it in GitHub Desktop.
Gzip a file using the zlib module
import zlib from "zlib";
import fs from "fs";
import stream from "stream";
import { promisify } from "util";
import readline from "readline";
const pipe = promisify(stream.pipeline);
const { stdin, stdout, stderr } = process;
(async function () {
const rl = readline.createInterface({ input: stdin, output: stdout });
const filename = await new Promise<string>((resolve, reject) =>
rl.question("What file would you like to compress? ", (file) => resolve(file)));
const input = fs.createReadStream(filename);
const output = fs.createWriteStream(`${filename}.gz`);
const gzip = zlib.createGzip();
await pipe(input, gzip, output);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment