8.2 用zip获得最好的压缩效果
-[0-9]
用zip
进行压缩时,可以调整其压缩率。zip
命令使用0
到9
来表示压缩率的度量标准:0
表示“一点也不压缩”(和tar相似,稍后会看到),1
表示“尽快完成压缩,而不需要压缩太多”,9
表示“尽可能压缩文件,不介意会为此等待较长的时间”。默认值是6
,但现在的计算机速度已经足够快,一直使用9
表示的压缩率也不会有什么问题。
假设你有兴趣研究Herman Melville的Moby-Dick,想收集一些关键的文字资料来帮助你理解这本书,如Moby-Dick、Milton的Paradise Lost。我们比较一下使用不同压缩率的结果。
$ ls -l
-rw-r--r-- scott scott 102519 job.txt
-rw-r--r-- scott scott 1236574 moby-dick.txt
-rw-r--r-- scott scott 508925 paradise_lost.txt
$ zip -0 moby.zip *.txt
adding: job.txt (stored 0%)
adding: moby-dick.txt (stored 0%)
adding: paradise_lost.txt (stored 0%)
$ ls -l
-rw-r--r-- scott scott 102519 job.txt
-rw-r--r-- scott scott 1236574 moby-dick.txt
-rw-r--r-- scott scott 1848444 moby.zip
-rw-r--r-- scott scott 508925 paradise_lost.txt
$ zip -1 moby.zip *txt
updating: job.txt (deflated 58%)
updating: moby-dick.txt (deflated 54%)
updating: paradise_lost.txt (deflated 50%)
$ ls -l
-rw-r--r-- scott scott 102519 job.txt
-rw-r--r-- scott scott 1236574 moby-dick.txt
-rw-r--r-- scott scott 869946 moby.zip
-rw-r--r-- scott scott 508925 paradise_lost.txt
$ zip -9 moby.zip *txt
updating: job.txt (deflated 65%)
updating: moby-dick.txt (deflated 61%)
updating: paradise_lost.txt (deflated 56%)
$ ls -l
-rw-r--r-- scott scott 102519 job.txt
-rw-r--r-- scott scott 1236574 moby-dick.txt
-rw-r--r-- scott scott 747730 moby.zip
-rw-r--r-- scott scott 508925 paradise_lost.txt
比较结果如表8-1所示。 表 8-1
图 书 | zip -0 | zip -1 | zip -9 |
---|---|---|---|
Moby-Dick | 0% | 54% | 61% |
Paradise Lost | 0% | 50% | 56% |
Job | 0% | 58% | 65% |
合计(以B为单位) | 1848444 | 869946 | 747730 |
从以上比较可以看出,压缩结果会随文件类型(文本文件的压缩效果通常比较好)和原始文件的大小而有所不同,让你对压缩效果有个大致的了解。除非你的计算机的速度真的很慢,或者确实没有耐心,否则的话可以一直使用-9
,获得最大的压缩率。
说明 如果想要更灵活些,可以在
.bashrc
文件中为zip
命令定义一个别名,如下所示:
alias zip='zip -9'
这样一来,就能一直使用
-9
的压缩率,而不用再刻意考虑它了。