zip是目前使用最多的压缩文件格式,在Linux服务器上也经常需要与zip文件进行交互。 我以前写了教程的Linux压缩和包文件,但只说了tar、tar.gz和tar.bz2。 这里继续写zip。 本来要把zip和rar写在一起,但是因为太长了,所以打算分开写。 ZIP文件格式为数据压缩和文档存储文件格式,原名Deflate,发明者为时尚茶,1989年1月发表了这种格式的资料。 ZIP通常使用扩展名“. ZIP”,MIME格式为应用程序/zip。 ZIP格式目前是几种主要的压缩格式之一,竞争对手包括RAR格式和开源7z格式。
这里使用的Linux版本是CentOS7,大多数系统通常都内置zip工具。 但是,一些最小安装的Linux没有zip工具。 压缩时如果显示-bash: zip:则找不到指示没有内置zip工具的命令,只需下载zip工具。
下载unzip:yum-yinstallunzip
zip yum-y安装zip下载
要了解更多有关如何使用yum的信息,请参阅Linux Yum的简单使用教程。
使用像Ubuntu这样的系统: apt-get install zip
如果在询问是否安装时输入y滑架,则Ubuntu在安装zip时也会自动安装unzip。
压缩
的压缩命令是压缩zip新文件名的文件名,将home目录下的1.txt和2.txt压缩为1.zip:[ root @ localhost home ] # zip1. zip1. txt2. txt
添加:1.txt (stored0% ) )。
Adding :2.txt (已禁用34 % ) )。
看看下面的文件。 [root@localhost home]# ls
1.txt 1.zip 2.txt
压缩当前目录中的所有文件: [ root @ localhost home ] # zip all.zip *
添加:1.txt (stored0% ) )。
添加:1.zip (stored0% ) )。
Adding :2.txt (已禁用34 % ) )。
将当前目录下的newdir目录和中的所有文件压缩为newdir.zip。 [ root @ localhost home ] # zip new dir.zip new dir
添加:新dir/(stored0% )。
将文件添加到zip包: zip -g zip包文件名要添加的文件名,1.zip添加3.txt:[ root @ localhost home ] # zip-G1.zip3. txt
添加:3.txt (stored0% ) )。
不直接打包和压缩: zip -0 zip包名称要打包的文件名,1.txt打包到file.zip:[ root @ localhost home ] # zip-0 file.zip1. txt
添加:1.txt (stored0% ) )。
高质量压缩: zip -9新文件名要压缩的文件名,以下三个txt文件为file.zip:[ root @ localhost home ] # zip-9 file.zip1. txt2. txt3. txt
u dating :1.txt (存储0 % ) )。
Adding :2.txt (已禁用34 % ) )。
添加:3.txt (stored0% ) )。
比较直接打包的文件的大小,确实有点小。 [root@localhost home]# ls -l
总使用量20
-rw-r----r----1路由232月22日153360041.txt
-rw-r----r----1路由6682月22日163360381.zip
-rw-r----r----1路由1452月22日153360072.txt
-rw-r----r----1路由642月22日163360223.txt
-rw-r----r----1rootroot6182月22日16336037file.zip
排除文件: zip -X新文件名要排除的文件名,将除2.txt以外的所有文件压缩为1.zip:[ root @ localhost home ] # zip-x1.zip2. txt
Adding :2.txt (已禁用34 % ) )。
加密: zip -e压缩新文件名的文件名。 按enter键后,系统会提示您输入密码。 1.txt加密压缩为1.zip:[ root @ localhost home ] # zip-E1.zip1. txt
p>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 所有,转载请联系博主获得授权。
如果对本文有什么问题或疑问都可以在评论区留言,我看到后会尽量解答。