无需登录 数据私有 本地保存

Brotli压缩率测试 - 文本/文件在线压缩

39
0
0
0

Brotli 压缩率测试

在线测试 Brotli 与 Gzip 压缩效果,对比压缩率,优化您的 Web 资源交付策略。

快速示例:
输入内容
拖放文件到此处,或 点击选择文件
支持 .txt / .html / .css / .js / .json / .xml 等文本文件
压缩设置 & 结果
0 11 11

输入文本或上传文件后点击压缩按钮查看结果

常见问题与知识点

什么是 Brotli 压缩算法?
Brotli 是由 Google 开发的一种通用无损压缩算法,于2015年发布。它结合了 LZ77 算法、霍夫曼编码和二阶上下文建模,特别适合压缩文本类数据(HTML、CSS、JavaScript 等)。Brotli 内置了一个包含常见 Web 字符串的静态字典(约120KB),这使其在压缩 Web 资源时比 Gzip 有显著优势。Brotli 已被所有主流浏览器和 CDN 服务广泛支持。
Brotli 与 Gzip 有什么区别?哪个更好?
Brotli 通常在压缩文本内容时比 Gzip 高出 15-25% 的压缩率,这意味着更小的文件体积和更快的传输速度。不过 Brotli 压缩速度较慢(尤其在高压缩级别时),而解压速度与 Gzip 相当。对于静态资源(可预先压缩),推荐使用 Brotli 最高级别;对于动态内容,可根据服务器性能选择 4-6 级别来平衡速度和压缩率。Gzip 的优势在于兼容性更广泛、压缩速度更快。
Brotli 压缩级别 0-11 如何选择?
  • 级别 0-3:快速压缩,压缩率较低,适合实时动态内容。
  • 级别 4-6:平衡模式,压缩率和速度均衡,适合大多数 Web 服务器场景。
  • 级别 7-9:高压缩率,适合静态资源预压缩。
  • 级别 10-11:最高压缩率,压缩速度最慢但体积最小,推荐用于可离线预压缩的静态文件(如生产构建产物)。
默认级别 11 可获得最佳压缩率,本工具默认使用 11 级以便您看到 Brotli 的最佳表现。
为什么 Brotli 对中文文本的压缩率也很高?
Brotli 的核心优势在于其强大的 LZ77 变体算法和上下文建模,这些技术不依赖特定语言。虽然 Brotli 内置的静态字典主要包含英文 Web 常用字符串,但其自适应字典和高效的模式匹配能力使其对中文、日文、韩文等非拉丁文字也有出色的压缩效果。实际测试中,Brotli 对 UTF-8 编码的中文文本通常能比 Gzip 多节省 10-20% 的体积。
哪些浏览器支持 Brotli 压缩?
所有现代浏览器均支持 Brotli(通过 br content-encoding):Chrome 51+、Firefox 44+、Safari 11+、Edge 15+、Opera 38+。全球浏览器支持率超过 97%。服务器可通过 Accept-Encoding 请求头自动检测客户端是否支持 Brotli,并相应地返回 brgzip 压缩的响应。
如何在 Nginx / Apache 中启用 Brotli?
Nginx:需要安装 ngx_brotli 模块(Google 官方提供),编译后配置 brotli on;brotli_comp_level 6;
Apache:使用 mod_brotli 模块,配置 AddOutputFilterByType BROTLI_COMPRESS text/html text/css application/javascript
CDN:Cloudflare、Fastly、KeyCDN 等主流 CDN 均默认支持 Brotli,无需额外配置。
什么类型的文件不适合 Brotli 压缩?
已经经过压缩处理的文件格式不适合再用 Brotli 压缩,因为压缩率极低甚至可能使体积变大。这些格式包括:JPEG、PNG、GIF、WebP(图片);MP4、WebM(视频);MP3、AAC(音频);ZIP、7z、RAR(压缩包);PDF(部分已压缩)。对于这些文件,直接传输原始文件即可,无需额外压缩。Brotli 最适合文本类内容:HTML、CSS、JavaScript、JSON、XML、SVG、字体文件(WOFF2 本身已使用 Brotli)、以及纯文本。

欢迎来到 Brotli 压缩测试

Brotli 是一种高效的压缩算法,特别适合 Web 内容的压缩传输。

© 2024 示例站点
`, css: `/* 现代 CSS 样式表 */ :root { --primary: #667eea; --secondary: #764ba2; --bg: #f8fafc; --text: #1a1a2e; --radius: 12px; } *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: 'Segoe UI', system-ui, sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; } .container { max-width: 1200px; margin: 0 auto; padding: 0 1.5rem; } .btn { display: inline-flex; align-items: center; gap: 0.5rem; padding: 0.75rem 1.5rem; border-radius: var(--radius); border: none; cursor: pointer; font-weight: 600; transition: all 0.2s; } .btn-primary { background: linear-gradient(135deg, var(--primary), var(--secondary)); color: #fff; box-shadow: 0 4px 12px rgba(102,126,234,0.3); } .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(102,126,234,0.4); } .card { background: #fff; border-radius: var(--radius); padding: 2rem; box-shadow: 0 1px 4px rgba(0,0,0,0.06); } @media (max-width: 768px) { .container { padding: 0 1rem; } }`, json: `{ "name": "brotli-compression-test", "version": "1.0.0", "description": "Brotli 压缩算法测试数据集", "author": "Google", "license": "MIT", "dependencies": { "brotli-wasm": "^1.3.1", "pako": "^2.1.0" }, "config": { "compression": { "algorithm": "brotli", "quality": 11, "mode": "text", "windowBits": 22 }, "server": { "host": "localhost", "port": 8080, "enableBrotli": true, "enableGzip": true, "staticCacheDays": 30 } }, "benchmarks": [ {"file": "index.html", "original": 15420, "brotli": 3420, "gzip": 4100}, {"file": "style.css", "original": 8750, "brotli": 1980, "gzip": 2450}, {"file": "bundle.js", "original": 128500, "brotli": 32100, "gzip": 38900} ] }`, lorem: `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Brotli compression is particularly effective for web content. It uses a large static dictionary that contains common strings found in HTML, CSS, and JavaScript files. This makes it especially suitable for compressing web resources. The algorithm combines LZ77, Huffman coding, and second-order context modeling to achieve excellent compression ratios. 在现代 Web 开发中,使用 Brotli 压缩可以显著减少传输数据量。与传统的 Gzip 相比,Brotli 通常能多节省 15-25% 的带宽。这对于移动端用户和网络条件较差的地区尤为重要。所有主流浏览器和 CDN 服务都已经支持 Brotli 压缩。 日本のウェブ開発においても、Brotliは非常に有用です。テキストコンテンツの圧縮率が高く、ページの読み込み速度を大幅に向上させることができます。多くの大手サイトがすでにBrotliを採用しています。` }; // ────────────────────────────────────── // Events // ────────────────────────────────────── $qualitySlider.on('input', function() { const val = $(this).val(); $qualityDisplay.text(val); $qualityBadge.text(val); }); $btnCompress.on('click', function() { doCompress(); }); $btnClear.on('click', function() { $input.val(''); fileBytes = null; fileName = null; $fileInfoArea.empty(); $resultsArea.hide(); $noResults.show(); lastCompressResult = null; $compressStatus.text(''); $('#stats-row').hide(); }); // Sample buttons $sampleBtns.on('click', '.btn-outline-sm', function() { const key = $(this).data('sample'); if (samples[key]) { $input.val(samples[key]); fileBytes = null; fileName = null; $fileInfoArea.empty(); $resultsArea.hide(); $noResults.show(); lastCompressResult = null; $compressStatus.text('已加载示例:' + $(this).text().trim() + ',点击压缩按钮测试'); $sampleBtns.find('.btn-outline-sm').removeClass('active-sample'); $(this).addClass('active-sample'); } }); // Drop zone $dropzone.on('click', function() { $fileInput.click(); }); $fileInput.on('change', function() { const file = this.files[0]; if (file) handleFile(file); }); $dropzone.on('dragover', function(e) { e.preventDefault(); $(this).addClass('drag-over'); }); $dropzone.on('dragleave', function() { $(this).removeClass('drag-over'); }); $dropzone.on('drop', function(e) { e.preventDefault(); $(this).removeClass('drag-over'); const file = e.originalEvent.dataTransfer.files[0]; if (file) handleFile(file); }); function handleFile(file) { fileName = file.name; const reader = new FileReader(); reader.onload = function(ev) { fileBytes = ev.target.result; // Try to display as text if it's a text file const textExtensions = ['.txt','.html','.htm','.css','.js','.json','.xml','.svg','.csv','.md','.log','.yml','.yaml','.php','.py','.rb','.java','.c','.cpp','.h','.ts','.tsx','.jsx']; const ext = '.' + fileName.split('.').pop().toLowerCase(); if (textExtensions.includes(ext) || file.type.startsWith('text/') || file.type === 'application/json' || file.type === 'application/xml') { try { const text = new TextDecoder().decode(fileBytes); $input.val(text); } catch(e) { $input.val('[二进制文件,无法预览文本内容]'); } } else { $input.val('[二进制文件:' + fileName + ',将基于原始字节进行压缩测试]'); } $fileInfoArea.html(' ' + fileName + ' (' + formatBytes(fileBytes.byteLength) + ') ×'); $('#remove-file').on('click', function() { fileBytes = null; fileName = null; $fileInfoArea.empty(); $input.val(''); $fileInput.val(''); $resultsArea.hide(); $noResults.show(); lastCompressResult = null; $compressStatus.text(''); }); $resultsArea.hide(); $noResults.show(); lastCompressResult = null; $compressStatus.text('文件已加载,点击压缩按钮测试').css('color','#374151'); }; reader.readAsArrayBuffer(file); } // Download buttons $(document).on('click', '#btn-download-br', function() { if (lastCompressResult && lastCompressResult.brotliData) { const name = fileName ? fileName + '.br' : 'compressed.br'; downloadBlob(lastCompressResult.brotliData, name); $compressStatus.text('✅ .br 文件已下载').css('color', '#059669'); } }); $(document).on('click', '#btn-download-gz', function() { if (lastCompressResult && lastCompressResult.gzipData) { const name = fileName ? fileName + '.gz' : 'compressed.gz'; downloadBlob(lastCompressResult.gzipData, name); $compressStatus.text('✅ .gz 文件已下载').css('color', '#059669'); } }); $(document).on('click', '#btn-copy-brotli-base64', function() { if (lastCompressResult && lastCompressResult.brotliData) { copyBase64(lastCompressResult.brotliData); } }); $(document).on('click', '#btn-copy-gzip-base64', function() { if (lastCompressResult && lastCompressResult.gzipData) { copyBase64(lastCompressResult.gzipData); } }); // FAQ accordion $(document).on('click', '#brotli-tool .faq-question', function() { const $item = $(this).parent('.faq-item'); $item.toggleClass('open'); // Close others $('#brotli-tool .faq-item').not($item).removeClass('open'); }); // Keyboard shortcut $(document).on('keydown', function(e) { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); doCompress(); } }); // ────────────────────────────────────── // Init // ────────────────────────────────────── initBrotli(); ensurePako().catch(() => { console.warn('pako not available, gzip comparison disabled'); }); $resultsArea.hide(); $('#stats-row').hide(); })();