Gzip Encode Decode
Compress text to gzip and decompress gzip back to text. Uses pako in the browser.
Back to all tools on ToolForge
Text
Gzip (Base64)
About Gzip Encode Decode
This gzip tool compresses text to gzip format and decompresses gzip data back to text. The compressed output is Base64-encoded for easy copy and paste. Uses the pako library in the browser.
Gzip Compression Algorithm
// Gzip compression using pako (DEFLATE algorithm)
function compress(text) {
const encoder = new TextEncoder();
const data = encoder.encode(text);
const compressed = pako.gzip(data);
// Convert to Base64 for safe text representation
return btoa(String.fromCharCode.apply(null, compressed));
}
// Gzip decompression
function decompress(base64) {
const binary = atob(base64);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
const decompressed = pako.ungzip(bytes);
return new TextDecoder().decode(decompressed);
}
Compression Ratio Examples
| Content Type | Original | Gzipped | Reduction |
|---|---|---|---|
| HTML document | 100 KB | 25 KB | ~75% |
| CSS stylesheet | 50 KB | 10 KB | ~80% |
| JSON data | 20 KB | 5 KB | ~75% |
| JavaScript file | 80 KB | 25 KB | ~69% |
Common Use Cases
- HTTP Compression: Enable gzip on web servers to reduce bandwidth (Content-Encoding: gzip)
- Log Compression: Compress log files for archival and storage efficiency
- Data Transmission: Reduce payload size for API requests and responses
- Frontend Optimization: Compress static assets (CSS, JS, HTML) for faster page loads
Frequently Asked Questions
- What is gzip compression?
- Gzip is a lossless compression format using the DEFLATE algorithm (LZ77 + Huffman coding). It can compress text by 70-90%, making it ideal for HTTP transmission. Gzip adds a header with metadata and CRC-32 checksum for data integrity verification.
- How does gzip compression work?
- Gzip uses the DEFLATE algorithm which combines LZ77 (finds repeated byte sequences) and Huffman coding (assigns shorter codes to frequent bytes). This two-stage approach efficiently compresses text, JSON, HTML, CSS, and other repetitive data patterns.
- When should I use gzip compression?
- Use gzip for: HTTP response compression (Content-Encoding: gzip), log file archival, reducing storage for text-based data, and speeding up data transfer. Avoid gzip for already-compressed formats like images, videos, or ZIP files.
- Why is the output Base64-encoded?
- Binary gzip data contains non-printable bytes that can't be safely displayed or copied as text. Base64 encoding converts binary to ASCII characters (A-Z, a-z, 0-9, +, /), making it safe for copy-paste, JSON embedding, and text-based storage.