Modern Web Guidance is now stable! Upgrade your coding agents with expert-vetted web development best practices.
Read the announcement.
Compression and decompression in the browser with the Compression Streams API
Stay organized with collections
Save and categorize content based on your preferences.
Write smaller web apps that don't need to ship their own compression or decompression library
The Compression Streams API
is for compressing and decompressing streams of data using the gzip or
deflate (or deflate-raw) formats.
With built in compression JavaScript applications don't need to include a compression
library, making the download size of the application smaller. Stable Chrome and Safari Technology Preview now
support this useful API. Compressing data is shown here.
const readableStream = await fetch('lorem.txt').then(
(response) => response.body
);
const compressedReadableStream = readableStream.pipeThrough(
new CompressionStream('gzip')
);
To decompress, pipe a compressed stream through the decompression stream.
const decompressedReadableStream = compressedReadableStream.pipeThrough(
new DecompressionStream('gzip')
);
Demo
Browser support
The Compression Streams API is supported from Chromium 80 and Safari Technology Preview 152.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-08-29 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-08-29 UTC."],[],[]]