8.2 用zip获得最好的压缩效果

-[0-9]

zip进行压缩时,可以调整其压缩率。zip命令使用09来表示压缩率的度量标准:0表示“一点也不压缩”(和tar相似,稍后会看到),1表示“尽快完成压缩,而不需要压缩太多”,9表示“尽可能压缩文件,不介意会为此等待较长的时间”。默认值是6,但现在的计算机速度已经足够快,一直使用9表示的压缩率也不会有什么问题。

假设你有兴趣研究Herman Melville的Moby-Dick,想收集一些关键的文字资料来帮助你理解这本书,如Moby-Dick、Milton的Paradise Lost。我们比较一下使用不同压缩率的结果。

  1. $ ls -l
  2. -rw-r--r-- scott scott 102519 job.txt
  3. -rw-r--r-- scott scott 1236574 moby-dick.txt
  4. -rw-r--r-- scott scott 508925 paradise_lost.txt
  5. $ zip -0 moby.zip *.txt
  6. adding: job.txt (stored 0%)
  7. adding: moby-dick.txt (stored 0%)
  8. adding: paradise_lost.txt (stored 0%)
  9. $ ls -l
  10. -rw-r--r-- scott scott 102519 job.txt
  11. -rw-r--r-- scott scott 1236574 moby-dick.txt
  12. -rw-r--r-- scott scott 1848444 moby.zip
  13. -rw-r--r-- scott scott 508925 paradise_lost.txt
  14. $ zip -1 moby.zip *txt
  15. updating: job.txt (deflated 58%)
  16. updating: moby-dick.txt (deflated 54%)
  17. updating: paradise_lost.txt (deflated 50%)
  18. $ ls -l
  19. -rw-r--r-- scott scott 102519 job.txt
  20. -rw-r--r-- scott scott 1236574 moby-dick.txt
  21. -rw-r--r-- scott scott 869946 moby.zip
  22. -rw-r--r-- scott scott 508925 paradise_lost.txt
  23. $ zip -9 moby.zip *txt
  24. updating: job.txt (deflated 65%)
  25. updating: moby-dick.txt (deflated 61%)
  26. updating: paradise_lost.txt (deflated 56%)
  27. $ ls -l
  28. -rw-r--r-- scott scott 102519 job.txt
  29. -rw-r--r-- scott scott 1236574 moby-dick.txt
  30. -rw-r--r-- scott scott 747730 moby.zip
  31. -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命令定义一个别名,如下所示:

  1. alias zip='zip -9'

这样一来,就能一直使用-9的压缩率,而不用再刻意考虑它了。