xinzip
v1.0.0
Published
High-performance streaming parallel compression library for large files (16GB+) in browsers
Maintainers
Readme
Xinzip - 高性能流式并行压缩库
专为大文件设计的浏览器端压缩库,支持16GB+文件的高效流式并行压缩。
✨ 特性
- 🚀 极速压缩:流式并行处理,充分利用多核CPU
- 💾 内存可控:始终控制在2GB内存以内
- 📁 大文件支持:理论支持无限大小文件(已测试16GB+)
- 🔧 自动优化:根据文件大小自动调整压缩策略
- 📊 实时监控:详细的进度和性能统计
- 🌐 纯浏览器:无需服务器,完全在浏览器端运行
📦 安装
npm install xinzip🚀 快速开始
基础用法
import Xinzip from 'xinzip';
// 创建压缩器实例
const compressor = new Xinzip({
compressionLevel: 6, // 压缩级别 1-9
maxWorkers: 4, // 最大Worker数量
onProgress: (progress, completed, total) => {
console.log(`压缩进度: ${progress.toFixed(1)}% (${completed}/${total})`);
}
});
// 压缩文件
async function compressFile(file) {
try {
// 初始化(只需要调用一次)
await compressor.init();
// 压缩
const result = await compressor.compress(file);
console.log('压缩完成:', result.stats);
console.log('原始大小:', result.stats.originalSize);
console.log('压缩后大小:', result.stats.compressedSize);
console.log('压缩率:', result.stats.compressionRatio);
console.log('压缩速度:', result.stats.speed);
// result.data 是压缩后的 Uint8Array
return result.data;
} catch (error) {
console.error('压缩失败:', error);
}
}
// 使用示例
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', async (e) => {
const file = e.target.files[0];
if (file) {
const compressedData = await compressFile(file);
// 下载压缩后的文件
const blob = new Blob([compressedData], { type: 'application/gzip' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = file.name + '.gz';
a.click();
URL.revokeObjectURL(url);
}
});高级配置
const compressor = new Xinzip({
chunkSize: 32 * 1024 * 1024, // 32MB 块大小
compressionLevel: 9, // 最高压缩级别
maxWorkers: 8, // 8个Worker并行
memoryLimit: 4 * 1024 * 1024 * 1024, // 4GB内存限制
onProgress: (progress, completed, total) => {
// 更新进度条
updateProgressBar(progress);
}
});支持的输入类型
// 文件对象
await compressor.compress(file);
// Blob对象
await compressor.compress(blob);
// ArrayBuffer
await compressor.compress(arrayBuffer);
// Uint8Array
await compressor.compress(uint8Array);📊 性能数据
| 文件大小 | 压缩速度 | 内存使用 | 压缩率 | |---------|----------|----------|--------| | 100MB | ~200MB/s | <500MB | ~70% | | 1GB | ~300MB/s | <1GB | ~75% | | 8GB | ~400MB/s | <2GB | ~80% | | 16GB | ~500MB/s | <2GB | ~85% |
测试环境:Chrome 120+, 8核CPU, 16GB内存
🔧 API 参考
Xinzip(options?)
创建压缩器实例。
选项
chunkSize?: number- 数据块大小,默认16MBcompressionLevel?: number- 压缩级别 1-9,默认6maxWorkers?: number- 最大Worker数量,默认为CPU核心数(最多8个)memoryLimit?: number- 内存限制,默认2GBonProgress?: Function- 进度回调(progress, completed, total) => void
方法
init(): Promise<void>- 初始化压缩器compress(input, options?): Promise<CompressionResult>- 压缩数据destroy(): void- 清理资源
🌐 浏览器兼容性
- Chrome 57+
- Firefox 52+
- Safari 11+
- Edge 16+
需要支持:
- WebAssembly
- Web Workers
- ES6 Modules
🤝 贡献
欢迎提交 Issue 和 Pull Request!
📄 许可证
MIT License
