在Linux系统中,"打包"和"压缩"是两个相关但不同的概念:
bash
tar -cvf archive.tar /path/to/files # 创建打包文件
tar -xvf archive.tar # 解包
bash
gzip file # 压缩(生成file.gz)
gzip -d file.gz # 解压
bash
bzip2 file # 压缩(生成file.bz2)
bunzip2 file.bz2 # 解压
bash
xz file # 压缩(生成file.xz)
unxz file.xz # 解压
bash
zip archive.zip file1 file2 # 压缩
unzip archive.zip # 解压
bash
7z a archive.7z /path/to/files # 压缩
7z x archive.7z # 解压
最常见的组合是使用tar打包后再压缩:
# 打包并gzip压缩
tar -czvf archive.tar.gz /path/to/files
# 打包并bzip2压缩
tar -cjvf archive.tar.bz2 /path/to/files
# 打包并xz压缩
tar -cJvf archive.tar.xz /path/to/files
工具 | 压缩速度 | 解压速度 | 压缩率 | 典型用途 |
---|---|---|---|---|
gzip | 快 | 快 | 中 | 日常使用 |
bzip2 | 中 | 慢 | 高 | 大文件压缩 |
xz | 慢 | 慢 | 最高 | 归档分发 |
zip | 中 | 快 | 中 | 跨平台共享 |
7z | 慢 | 中 | 最高 | 高压缩需求 |
多线程压缩:
pigz -k file # gzip多线程版
pbzip2 -k file # bzip2多线程版
分卷压缩:
tar -czvf - /path/to/files | split -b 2G - archive.tar.gz.
排除特定文件:
tar -czvf archive.tar.gz --exclude='*.tmp' /path/to/files
查看压缩文件内容:
tar -tzf archive.tar.gz
压缩率测试:
time tar -czf test.tar.gz /path/to/files
time tar -cjf test.tar.bz2 /path/to/files
time tar -cJf test.tar.xz /path/to/files
希望这份比较能帮助您根据具体需求选择最合适的打包压缩工具。