8.1 用zip归档和压缩文件
zip
zip
既可以对文件进行归档,也可以对文件进行压缩,这样有利于将多个文件作为电子邮件的附件发送、备份文件,或节约磁盘空间。使用zip
很简单。假设你想通过邮件把一个TIFF文件发送给某个人,TIFF图像没有经过压缩,它的体积可能非常大。压缩它,应该可以让电子邮件的附件变得小一些。
说明 在使用
ls -l
命令时,只显示对每个例子来说必要的信息。
$ ls -lh
-rw-r--r-- scott scott 1006K young_edgar_scott.tif
$ zip grandpa.zip young_edgar_scott.tif
adding: young_edgar_scott.tif (deflated 19%)
$ ls -lh
-rw-r--r-- scott scott 1006K young_edgar_scott.tif
-rw-r--r-- scott scott 819K grandpa.zip
➥grandpa.zip
在这个例子中,正如zip命令显示的信息所示,生成的zip文件的体积缩减了大约200 KB(或缩小了19%)。这样做很棒!而且还可以一次压缩多个图像。
$ ls -l
-rw-r--r-- scott scott 251980 edgar_intl_shoe.tif
-rw-r--r-- scott scott 1130922 edgar_baby.tif
-rw-r--r-- scott scott 1029224 young_edgar_scott.tif
$ zip grandpa.zip edgar_intl_shoe.tif edgar_
➥baby.tif young_edgar_scott.tif
adding: edgar_intl_shoe.tif (deflated 4%)
adding: edgar_baby.tif (deflated 12%)
adding: young_edgar_scott.tif (deflated 19%)
$ ls -l
-rw-r--r-- scott scott 251980 edgar_intl_shoe.tif
-rw-r--r-- scott scott 1130922 edgar_baby.tif
-rw-r--r-- scott scott 2074296 grandpa.zip
-rw-r--r-- scott scott 1029224 young_edgar_scott.tif
不过,用这种方法压缩单独的文件并不是很优雅。压缩3个文件时,这种方法还不算糟糕。收到压缩文件的人解压grandpa.zip
文件,最后得到3个单独的文件。但是如果一个压缩文件包含了50个文件,用户解压后得到的文件将放置得很零乱。最好是压缩包含这50个文件的目录,这样用户解压以后,得到的将是一个清爽的目录。
$ ls -lF
drwxr-xr-x scott scott edgar_scott/
$ zip grandpa.zip edgar_scott
adding: edgar_scott/ (stored 0%)
adding: edgar_scott/edgar_baby.tif (deflated 12%)
adding: edgar_scott/young_edgar_scott.tif (deflated 19%)
adding: edgar_scott/edgar_intl_shoe.tif (deflated 4%)
$ ls -lF
drwxr-xr-x scott scott 160 edgar_scott/
-rw-r--r-- scott scott 2074502 grandpa.zip
不管压缩一个文件、多个文件,还是目录,用法模式都是相同的:zip
命令,后面跟着想要创建的Zip文件的名称,最后是想要加到Zip文件的项目内容。