Gzip Compress and Decompress CLI
A Node CLI that gzips stdin or a file to stdout and decompresses with gunzip, built on the streaming zlib API for bounded memory use.
Project
node-gzip-cli
Entry point: gz.mjs
Install
node --versionRun
node gz.mjs compress file.txt > file.txt.gzTest
node --test gz.test.mjsExpected output
compress writes a gzip stream that gunzip restores byte-for-byte; the round-trip output is identical to the input.Dependencies
- node >=20
What it does
Pipes input through a gzip or gunzip transform and writes the result to stdout. Streaming keeps memory flat regardless of file size, which matters for large archives.
import { createGzip, createGunzip } from 'node:zlib';
import { createReadStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
const [mode, file] = process.argv.slice(2);
if (mode !== 'compress' && mode !== 'decompress') { console.error('usage: gz <compress|decompress> [file]'); process.exit(1); }
const input = file ? createReadStream(file) : process.stdin;
const transform = mode === 'compress' ? createGzip() : createGunzip();
await pipeline(input, transform, process.stdout);
Run and verify
node gz.mjs compress file.txt > file.txt.gz
Run the test command to check the implementation: node --test gz.test.mjs.
Expected Output
Compressing then decompressing a file yields the original bytes. Comparing the two with cmp prints nothing and exits 0:
(no output, exit 0)
Limitations
Uses gzip default compression, which favors speed over ratio. For archival, raise the level and consider a stronger format such as zstd or xz.