在Windows中,我们用得最多的解压缩软件就是WinRAR了,这个软件对我们实现解压缩非常方便。但是如果在Linux系统中,我们还可以尝试用命令来实现对文件的解压缩,这篇文章主要介绍在Linux中用zip命令来压缩和解压缩文件。zip命令非常强大,下面我们一步步来介绍。
1、 zip命令基础
怎样用zip压缩多个文件呢?
语法: zip {.zipfile-name} {file-names}
#zip var-log-files.zip /var/log/*
adding: var/log/acpid (deflated 81%)
adding: var/log/anaconda.log (deflated 79%)
adding: var/log/anaconda.syslog (deflated 73%)
adding: var/log/anaconda.xlog (deflated 82%)
adding: var/log/audit/ (stored 0%) adding: var/log/boot.log (stored 0%)
adding: var/log/boot.log.1 (deflated 40%)
adding: var/log/boot.log.2 (deflated 42%)
adding: var/log/boot.log.3 (deflated 40%)
adding: var/log/boot.log.4 (deflated 40%)
如何递归地压缩一个目录及目录下的文件?
#zip -r var-log-dir.zip /var/log/
updating: var/log/ (stored 0%)
adding: var/log/wtmp (deflated 78%)
adding: var/log/scrollkeeper.log (deflated 94%)
adding: var/log/rpmpkgs.3 (deflated 68%)
adding: var/log/spooler (stored 0%)
adding: var/log/cron.2 (deflated 90%)
adding: var/log/spooler.1 (stored 0%)
adding: var/log/spooler.4 (stored 0%)
adding: var/log/httpd/ (stored 0%)
adding: var/log/rpmpkgs.1 (deflated 68%)
adding: var/log/anaconda.log (deflated 79%)
adding: var/log/secure.2 (deflated 93%)
如何解压a*.zip的压缩包?
# unzip var-log.zip
Archive: var-log.zip
inflating: var/log/acpid
inflating:var/log/anaconda.log
inflating: var/log/anaconda.syslog
inflating: var/log/anaconda.xlog
creating: var/log/audit/
#p#
用参数v查看解压缩过程中的详细信息:
# unzip -v var-log.zip
Archive:var-log.zip
Length Method Size Ratio Date Time CRC-32 Name
------ ------ ----- ----- ---- ---- ------ ----
1916 Defl:N 369 81% 02-08-0814:27 e2ffdc0cvar/log/acpid
13546 Defl:N 2900 79% 02-02-07 14:25 34cc03a1 var/log/anaconda.log
skip..
7680 Defl:N 411 95% 12-30-08 10:55 fe876ee9 var/log/wtmp.1
40981 Defl:N 7395 82% 02-08-08 14:28 6386a95e var/log/Xorg.0.log
-------- ------- --- --- ----
41406991 2809229 93% 56 files
如何在不解压一个压缩包的情况下看里面的文件呢?
#unzip -l var-log.zip
Archive: var-log.zip
Length Date Time Name
-------- ---- ---- ----
1916 02-08-08 14:27 var/log/acpid
13546 02-02-07 14:25 var/log/anaconda.log
..skip..
40981 02-08-08 14:28 var/log/Xorg.0.log
40981 02-08-07 14:56 var/log/Xorg.0.log.old
-------- -------
41406991 56 files
2、 zip命令高级用法
zip命令提供了十个压缩等级:
o等级0是最低等级,只做归档,不压缩
o等级1压缩率低,但速度很快
o等级6是默认的压缩等级
o等级9的压缩率最高,但它耗时也多,除了大文件,我们一般推荐于用等级9
下面的例子中我分别利用等级0,等级6,等级9压缩同样的一个目录,看看他们分别压缩后的大小:
# zip var-log-files-default.zip /var/log/*
# zip -0 var-log-files-0.zip /var/log/*
# zip -9 var-log-files-9.zip /var/log/*
# ls -ltr
-rw-r--r-- 1 root root 2817248 Jan 1 13:05
var-log-files-default.zip
-rw-r--r-- 1 root root 41415301 Jan 1 13:05
var-log-files-0.zip
-rw-r--r-- 1 root root 2582610 Jan 1 13:06
var-log-files-9.zip
#p#
3、zip文件的密码保护
使用zip命令的P选项来加密zip文件
#zip -P mysecurepwd var-log-protected.zip /var/log/*
在shell脚本中使用上述选项来做后台工作是个不错的选择。但在使用交互式命令行时,你一般不会想让密码在History中可见。所以,这个时候就要使用下述的e选项来设定密码了。
# zip -e var-log-protected.zip /var/log/*
Enter password:
Verify password:
updating: var/log/acpid (deflated 81%)
updating: var/log/anaconda.log (deflated 79%)
当你要解压缩一个有密码保护的压缩文件时,会被要求输入密码。
# unzip var-log-protected.zip
Archive: var-log-protected.zip
[var-log-protected.zip] var/log/acpid password:
4、 检查zip文件的完整性
有时候你想要检查zip文件的完整性又不想解压它。这时可以使用如下所述的t选项
# unzip -t var-log.zip
Archive: var-log.zip
testing: var/log/acpid OK
testing: var/log/anaconda.log OK
testing: var/log/anaconda.syslog OK
skip...
testing: var/log/wtmp OK
testing: var/log/wtmp.1 OK
testing: var/log/Xorg.0.log OK
No errors detected in compressed data of var-log.zip.
【编辑推荐】