zip 是目前使用最多的压缩文件格式,哪怕是 Linux 服务器也经常需要和 zip 文件打交道。之前我写过一篇教程 Linux压缩和打包文件,只说到了 tar、tar.gz、tar.bz2,这里就继续来写 zip,本来是要把 zip 和 rar 写在一起的,但是因为太长,所以准备分开写。ZIP文件格式是一种数据压缩和文档储存的文件格式,原名Deflate,发明者为菲尔·卡茨,他于1989年1月公布了该格式的资料。ZIP通常使用后缀名“.zip”,它的MIME格式为application/zip。目前,ZIP格式属于几种主流的压缩格式之一,其竞争者包括RAR格式以及开放源码的7z格式。
我这里使用的 Linux 版本是 CentOS7,一般大多数系统都会内置有 zip 工具,不过一些最小安装的 Linux 可能没有 zip 工具,如果压缩时出现 -bash: zip: 未找到命令 说明没有内置 zip 工具,只需要下载 zip 工具即可。
下载 unzip:yum -y install unzip
下载 zipyum -y install zip
如果要更详细的了解 yum 的使用可以看 Linux Yum简单使用教程 。
如果是 Ubuntu 这一类系统的话使用:apt-get install zip
在询问是否安装的时候输入 y 回车,Ubuntu 在安装 zip 的时候也会自动安装 unzip。
zip 压缩
压缩的命令为: zip 新 文件名 要压缩的文件名 ,下面把 home 目录下的 1.txt 和 2.txt 压缩为 1.zip:[root@localhost home]# zip 1.zip 1.txt 2.txt
adding: 1.txt (stored 0%)
adding: 2.txt (deflated 34%)
查看一下文件:[root@localhost home]# ls
1.txt 1.zip 2.txt
压缩当前目录下的所有文件:[root@localhost home]# zip all.zip *
adding: 1.txt (stored 0%)
adding: 1.zip (stored 0%)
adding: 2.txt (deflated 34%)
把当前目录下的 newdir 目录和里面所有的文件都压缩到 newdir.zip:[root@localhost home]# zip newdir.zip newdir
adding: newdir/ (stored 0%)
给 zip 包添加文件: zip -g zip包文件名 要添加的文件名 ,下面给 1.zip 中添加一个 3.txt:[root@localhost home]# zip -g 1.zip 3.txt
adding: 3.txt (stored 0%)
直接打包不压缩: zip -0 zip包名 要打包的文件名 ,下面把 1.txt 打包为 file.zip:[root@localhost home]# zip -0 file.zip 1.txt
adding: 1.txt (stored 0%)
高质量压缩: zip -9 新文件名 要压缩的文件名 ,下面把 3 个 txt 文件压缩为 file.zip:[root@localhost home]# zip -9 file.zip 1.txt 2.txt 3.txt
updating: 1.txt (stored 0%)
adding: 2.txt (deflated 34%)
adding: 3.txt (stored 0%)
对比一下直接打包的文件体积却确实小了一点点[root@localhost home]# ls -l
总用量 20
-rw-r--r--. 1 root root 23 2月 22 15:04 1.txt
-rw-r--r--. 1 root root 668 2月 22 16:38 1.zip
-rw-r--r--. 1 root root 145 2月 22 15:07 2.txt
-rw-r--r--. 1 root root 64 2月 22 16:22 3.txt
-rw-r--r--. 1 root root 618 2月 22 16:37 file.zip
排除文件: zip -X 新文件名 要排除的文件名 ,下面把除 2.txt 外的所有文件都压缩到 1.zip:[root@localhost home]# zip -X 1.zip 2.txt
adding: 2.txt (deflated 34%)
加密压缩: zip -e 新文件名 要压缩的文件名 ,按下回车后会提示输入密码,下面把 1.txt 加密压缩为 1.zip:[root@localhost home]# zip -e 1.zip 1.txt
Enter password:
Verify password:
adding: 1.txt (stored 0%)
删除 zip 包中的文件: zip -d zip包名 要删除的文件名 ,下面删除 1.zip 中的 1.txt 文件:[root@localhost home]# zip -d 1.zip 1.txt
deleting: 1.txt
下面是 zip 的一些参数说明:参数说明-d删除zip包中的文件
-e加密压缩
-F修复zip文件
-g向zip包中添加文件
-h显示zip帮助
-h2显示更多的zip帮助
-j只压缩文件,不处理目录,压缩一个目录只会包含文件,不包含目录
-L显示zip许可证
-m压缩完成后删除原文件
-o把zip包的修改时间改为zip包中文件的最新时间
-p加密压缩,明文存储密码,安全性不如 -e
-q压缩文件时不输出任何信息
-T测试zip文件的完整性
-u更新文件
-X排除文件
-z给zip包添加注释信息
-0 or -1 or -2 or -3 or -4 or -5 or -6 or -7 or -8 or -9控制压缩的质量,质量越好压缩速度越慢,-0不压缩,-9质量最好,默认为-6
unzip 解压
解压 zip 包到当前目录: unzip zip包文件名 ,下面解压 1.zip:[root@localhost home]# unzip 1.zip
Archive: 1.zip
extracting: 1.txt
inflating: 2.txt
extracting: 3.txt
把文件解压到其他目录: unzip -d 目录名 zip包文件名 ,下面把 home 目录下的 1.zip 解压到 root 目录:[root@localhost home]# unzip -d /root 1.zip
Archive: 1.zip
extracting: /root/1.txt
inflating: /root/2.txt
extracting: /root/3.txt
查看 zip 包内有哪些文件: unzip -l zip包文件名 ,下面查看 1.zip 内有哪些文件:[root@localhost home]# unzip -l 1.zip
Archive: 1.zip
Length Date Time Name
--------- ---------- ----- ----
23 02-22-2019 15:04 1.txt
145 02-22-2019 15:07 2.txt
64 02-22-2019 16:22 3.txt
--------- -------
232 3 files
查看 zip 包内文件的压缩率: unzip -v zip包文件名 ,下面查看 1.zip 内文件的压缩率:[root@localhost home]# unzip -v 1.zip
Archive: 1.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
23 Stored 23 0% 02-22-2019 15:04 d7df0560 1.txt
145 Defl:N 95 35% 02-22-2019 15:07 ad661f88 2.txt
64 Stored 64 0% 02-22-2019 16:22 bc70d4fa 3.txt
-------- ------- --- -------
232 182 22% 3 files
查看文件是否有损坏: unzip -t zip包文件名 ,下面查看 1.zip 是否有文件损坏:[root@localhost home]# unzip -t 1.zip
Archive: 1.zip
testing: 1.txt OK
testing: 2.txt OK
testing: 3.txt OK
No errors detected in compressed data of 1.zip.
下面是 unzip 的参数说明:参数说明-c将解压缩的结果显示到屏幕上,并对字符做适当的转换。
-f更新现有的文件。
-l显示压缩文件内所包含的文件。
-p与-c参数类似,会将解压缩的结果显示到屏幕上,但不会执行任何的转换。
-t检查压缩文件是否正确。
-u与-f参数类似,但是除了更新现有的文件外,也会将压缩文件中的其他文件解压缩到目录中。
-v查看压缩包内文件的详细信息。
-z查看压缩包注释。
-j不处理压缩文件中原有的目录路径。
-L将压缩文件中的全部文件名改为小写。
-n解压缩时不要覆盖原有的文件。
-o不必先询问用户,unzip执行后覆盖原有文件。
-q执行时不显示任何信息。
-d解压到指定目录。
以上就是 Linux zip和 unzip 的使用教程,一个是压缩,一个是解压,两个缺一不可,无论是 CentOS 还是 Ubuntu 命令都是一样的。
相关文章:
版权声明:本文为原创文章,版权归 Mr. Ma's Blog 所有,转载请联系博主获得授权。
如果对本文有什么问题或疑问都可以在评论区留言,我看到后会尽量解答。