Vanilla JavaScript Projects

Project 45, Hardware and performance

Browser File Compressor

Drop a file to gzip it, or drop a .gz file to unpack it. The browser's own compression streams do the work, so big files never get fully loaded into memory.

.gz
Main API
CompressionStream
Formats
gzip, deflate, deflate-raw
Dependencies
None
Your browser
Checking

Compress or decompress

Drop any file to compress it, or a .gz file to unpack it.
Waiting for a file

Or try with text

Result

-

Sizes appear here.

Before
-
After
-

Photos, videos and zip files are already compressed, so gzip saves little on them. Text, CSV, JSON and code shrink a lot.

How it works

  1. Stream the filefile.stream() gives a ReadableStream, so the file is read piece by piece instead of all at once.
  2. Pipe through gzippipeThrough(new CompressionStream("gzip")) compresses each piece as it passes. A small TransformStream counts bytes for the progress bar.
  3. Collectnew Response(stream).blob() gathers the output into a file you can download.
async function gzip(file, onProgress) {
  let done = 0;
  const counter = new TransformStream({
    transform(chunk, ctl) { done += chunk.byteLength; onProgress(done / file.size); ctl.enqueue(chunk); },
  });
  const stream = file.stream()
    .pipeThrough(counter)
    .pipeThrough(new CompressionStream("gzip"));
  return new Response(stream).blob();                  // collect the output
}
// Unpack: file.stream().pipeThrough(new DecompressionStream("gzip"))