2023年8月3日发(作者:)

linux(centos8):使⽤zipunzip压缩和解压缩⽂件⼀,查看zip命令所属的rpm包1,zip[root@kubemaster ~]# whereis zipzip: /usr/bin/zip /usr/share/man/man1/[root@kubemaster ~]# rpm -qf /usr/bin/8.x86_64

如果找不到zip命令,可以⽤dnf进⾏安装[root@kubemaster ~]# dnf install zip

2,unzip[root@kubemaster ~]# whereis unzipunzip: /usr/bin/unzip /usr/share/man/man1/[root@kubemaster ~]# rpm -qf /usr/bin/8.x86_64

如果找不到unzip命令,可以⽤dnf进⾏安装[root@kubemaster ~]# dnf install unzip

说明:刘宏缔的架构森林是⼀个专注架构的博客,地址: 对应的源码可以访问这⾥获取:

说明:作者:刘宏缔 邮箱: 371125307@

⼆,查看zip命令的版本和帮助:1,zip直接输⼊命令后会打印版本和帮助信息[root@kubemaster ~]# zipCopyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software 3.0 (July 5th 2008). Usage:zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list] The default action is to add or replace zipfile entries from list, which can include the special name - to compress

2,unzip:直接输⼊命令后会打印版本和帮助信息[root@kubemaster ~]# unzipUnZip 6.00 of 20 April 2009, by Info-ZIP. Maintained by C. Spieler. Sendbug reports using /; see README for : unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir] Default action is to extract files in list, except those in xlist, to exdir;...

三,zip的常⽤例⼦1,把⽂件打包压缩进zip⽂件[root@kubemaster zip]# zip adding: (deflated 82%) adding: (deflated 82%)

2,查看zip包中包含的⽂件:⽤zip查看#-sf:--show-files:显⽰⽂件列表[root@kubemaster zip]# zip -sf

Archive contains: al 2 entries (112 bytes)

⽤unzip查看#-l:列出⽂件[root@kubemaster zip]# unzip -l hive: Length Date Time Name--------- ---------- ----- ---- 56 07-28-2020 18:38 56 07-28-2020 18:38 --------- ------- 112 2 files

⽤zipinfo查看:[root@kubemaster zip]# zipinfo

Archive: file size: 318 bytes, number of entries: 2-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 2 files, 112 bytes uncompressed, 20 bytes compressed: 82.1%说明:zipinfo是unzip包中⾃带的命令

3,添加⼀个⽂件到现有的zip包[root@kubemaster zip]# zip adding: (deflated 82%)[root@kubemaster zip]# zipinfo

Archive: file size: 466 bytes, number of entries: 3-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 3 files, 168 bytes uncompressed, 30 bytes compressed: 82.1%可以看到已添加成功

4,从现有的zip压缩包中删除⼀个⽂件[root@kubemaster zip]# zip -d eting: [root@kubemaster zip]# zipinfo

Archive: file size: 318 bytes, number of entries: 2-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 2 files, 112 bytes uncompressed, 20 bytes compressed: 82.1%可以看到已删除成功 5,添加到zip时去掉原⽬录,只保留⽂件名:#-j:不处理压缩⽂件中原有的⽬录路径[root@kubemaster zip]# zip -j /var/log/cron adding: cron (deflated 86%)[root@kubemaster zip]# zipinfo

Archive: file size: 1559 bytes, number of entries: 4-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 -rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron4 files, 7141 bytes uncompressed, 987 bytes compressed: 86.2%可以看到cron已经被去掉了⽬录信息

6,替换zip包中的⽂件:[root@kubemaster zip]# zipinfo

Archive: file size: 1559 bytes, number of entries: 4-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 -rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron4 files, 7141 bytes uncompressed, 987 bytes compressed: 86.2%可以看到的⽂件⼤⼩是56我们修改⽂件后,替换压缩包中的此⽂件[root@kubemaster zip]# zip -u [root@kubemaster zip]# zipinfo

Archive: file size: 1564 bytes, number of entries: 4-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 -rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron4 files, 7337 bytes uncompressed, 992 bytes compressed: 86.5%已替换成功

7,指定压缩⽐时,默认的压缩⽐是多少?[root@kubemaster zip]# zip -h2... -0 store files (no compression) -1 to -9 compress fastest to compress best (default is 6)...这个值⼀般不需要改动

8,压缩⼀个⽬录:# -r :recurse into directories递归压缩⽬录[root@kubemaster goods]# [root@kubemaster goods]# cd ..[root@kubemaster zip]# zip -r ./goods/ adding: goods/ (stored 0%) adding: goods/ (deflated 71%) adding: goods/ (deflated 74%)[root@kubemaster zip]# zipinfo

Archive: file size: 2020 bytes, number of entries: 7-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 -rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 crondrwxr-xr-x 3.0 unx 0 bx stor 20-Jul-31 11:22 goods/-rw-r--r-- 3.0 unx 21 tx defN 20-Jul-31 11:22 goods/-rw-r--r-- 3.0 unx 23 tx defN 20-Jul-31 11:22 goods/7 files, 7381 bytes uncompressed, 1004 bytes compressed: 86.4% 四,例⼦:zip包加密码1,加密码:#-P:指定添加⽂件的密码,解压此⽂件时会要求输⼊注意:是此命令⾏中所添加的⽂件有密码,不是给整个zip包加的密码多个⽂件可以分别对应多个不同的密码[root@kubemaster zip]# zip -P pass -r goods adding: goods/ (stored 0%) adding: goods/ (deflated 71%) adding: goods/ (deflated 74%)

2,解压时会要求输⼊密码:[root@kubemaster zip]# unzip

Archive: [] goods/ password:

五,例⼦:zip包加备注 :1,添加备注:[root@kubemaster zip]# zip -z er new zip file comment (end with .):goods list zipimportant files..

2,查看zip⽂件中已添加的注释[root@kubemaster zip]# zipnote

...goods list zipimportant files.

3,⽤zipinfo也可以查看注释# -z: 打印注释[root@kubemaster zip]# zipinfo -z

Archive: ds list zipimportant file size: 2052 bytes, number of entries: 7-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 -rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 crondrwxr-xr-x 3.0 unx 0 bx stor 20-Jul-31 11:22 goods/-rw-r--r-- 3.0 unx 21 tx defN 20-Jul-31 11:22 goods/-rw-r--r-- 3.0 unx 23 tx defN 20-Jul-31 11:22 goods/7 files, 7381 bytes uncompressed, 1004 bytes compressed: 86.4%

六,unzip的常⽤例⼦1,验证压缩包是否完整

#-t:test compressed archive data[root@kubemaster zip]# unzip -t hive: testing: goods/ OK[] goods/ password:

testing: goods/ OK testing: goods/ OKNo errors detected in compressed data of . 2,解压缩到指定⽬录:#-d:指定要解压到的⽬录[root@kubemaster zip]# unzip -d /root/unzipArchive: creating: /root/unzip/goods/[] goods/ password:

inflating: /root/unzip/goods/

inflating: /root/unzip/goods/

[root@kubemaster zip]# ls /root/unzip/goods

3,解压缩时取消⽬录:#-j:忽略⽂件原有⽬录,把⽂件统⼀解压到第⼀级⽬录[root@kubemaster zip]# unzip -j -d /root/undirArchive: [] goods/ password:

inflating: /root/undir/

inflating: /root/undir/

[root@kubemaster zip]# ls /root/

4,解压缩指定的⽂件说明:把要解压的⽂件名写到命令⾏中zip包后⾯即可[root@kubemaster zip]# unzip goods/ -d /root/undir2Archive: [] goods/ password:

inflating: /root/undir2/goods/

[root@kubemaster zip]# ls -R /root/undir2/root/undir2:goods/root/undir2/goods:

七,查看linux的版本:[root@kubemaster ~]# cat /etc/redhat-release

CentOS Linux release 8.2.2004 (Core)

2023年8月3日发(作者:)

linux(centos8):使⽤zipunzip压缩和解压缩⽂件⼀,查看zip命令所属的rpm包1,zip[root@kubemaster ~]# whereis zipzip: /usr/bin/zip /usr/share/man/man1/[root@kubemaster ~]# rpm -qf /usr/bin/8.x86_64

如果找不到zip命令,可以⽤dnf进⾏安装[root@kubemaster ~]# dnf install zip

2,unzip[root@kubemaster ~]# whereis unzipunzip: /usr/bin/unzip /usr/share/man/man1/[root@kubemaster ~]# rpm -qf /usr/bin/8.x86_64

如果找不到unzip命令,可以⽤dnf进⾏安装[root@kubemaster ~]# dnf install unzip

说明:刘宏缔的架构森林是⼀个专注架构的博客,地址: 对应的源码可以访问这⾥获取:

说明:作者:刘宏缔 邮箱: 371125307@

⼆,查看zip命令的版本和帮助:1,zip直接输⼊命令后会打印版本和帮助信息[root@kubemaster ~]# zipCopyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software 3.0 (July 5th 2008). Usage:zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list] The default action is to add or replace zipfile entries from list, which can include the special name - to compress

2,unzip:直接输⼊命令后会打印版本和帮助信息[root@kubemaster ~]# unzipUnZip 6.00 of 20 April 2009, by Info-ZIP. Maintained by C. Spieler. Sendbug reports using /; see README for : unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir] Default action is to extract files in list, except those in xlist, to exdir;...

三,zip的常⽤例⼦1,把⽂件打包压缩进zip⽂件[root@kubemaster zip]# zip adding: (deflated 82%) adding: (deflated 82%)

2,查看zip包中包含的⽂件:⽤zip查看#-sf:--show-files:显⽰⽂件列表[root@kubemaster zip]# zip -sf

Archive contains: al 2 entries (112 bytes)

⽤unzip查看#-l:列出⽂件[root@kubemaster zip]# unzip -l hive: Length Date Time Name--------- ---------- ----- ---- 56 07-28-2020 18:38 56 07-28-2020 18:38 --------- ------- 112 2 files

⽤zipinfo查看:[root@kubemaster zip]# zipinfo

Archive: file size: 318 bytes, number of entries: 2-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 2 files, 112 bytes uncompressed, 20 bytes compressed: 82.1%说明:zipinfo是unzip包中⾃带的命令

3,添加⼀个⽂件到现有的zip包[root@kubemaster zip]# zip adding: (deflated 82%)[root@kubemaster zip]# zipinfo

Archive: file size: 466 bytes, number of entries: 3-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 3 files, 168 bytes uncompressed, 30 bytes compressed: 82.1%可以看到已添加成功

4,从现有的zip压缩包中删除⼀个⽂件[root@kubemaster zip]# zip -d eting: [root@kubemaster zip]# zipinfo

Archive: file size: 318 bytes, number of entries: 2-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 2 files, 112 bytes uncompressed, 20 bytes compressed: 82.1%可以看到已删除成功 5,添加到zip时去掉原⽬录,只保留⽂件名:#-j:不处理压缩⽂件中原有的⽬录路径[root@kubemaster zip]# zip -j /var/log/cron adding: cron (deflated 86%)[root@kubemaster zip]# zipinfo

Archive: file size: 1559 bytes, number of entries: 4-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 -rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron4 files, 7141 bytes uncompressed, 987 bytes compressed: 86.2%可以看到cron已经被去掉了⽬录信息

6,替换zip包中的⽂件:[root@kubemaster zip]# zipinfo

Archive: file size: 1559 bytes, number of entries: 4-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 -rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron4 files, 7141 bytes uncompressed, 987 bytes compressed: 86.2%可以看到的⽂件⼤⼩是56我们修改⽂件后,替换压缩包中的此⽂件[root@kubemaster zip]# zip -u [root@kubemaster zip]# zipinfo

Archive: file size: 1564 bytes, number of entries: 4-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 -rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron4 files, 7337 bytes uncompressed, 992 bytes compressed: 86.5%已替换成功

7,指定压缩⽐时,默认的压缩⽐是多少?[root@kubemaster zip]# zip -h2... -0 store files (no compression) -1 to -9 compress fastest to compress best (default is 6)...这个值⼀般不需要改动

8,压缩⼀个⽬录:# -r :recurse into directories递归压缩⽬录[root@kubemaster goods]# [root@kubemaster goods]# cd ..[root@kubemaster zip]# zip -r ./goods/ adding: goods/ (stored 0%) adding: goods/ (deflated 71%) adding: goods/ (deflated 74%)[root@kubemaster zip]# zipinfo

Archive: file size: 2020 bytes, number of entries: 7-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 -rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 crondrwxr-xr-x 3.0 unx 0 bx stor 20-Jul-31 11:22 goods/-rw-r--r-- 3.0 unx 21 tx defN 20-Jul-31 11:22 goods/-rw-r--r-- 3.0 unx 23 tx defN 20-Jul-31 11:22 goods/7 files, 7381 bytes uncompressed, 1004 bytes compressed: 86.4% 四,例⼦:zip包加密码1,加密码:#-P:指定添加⽂件的密码,解压此⽂件时会要求输⼊注意:是此命令⾏中所添加的⽂件有密码,不是给整个zip包加的密码多个⽂件可以分别对应多个不同的密码[root@kubemaster zip]# zip -P pass -r goods adding: goods/ (stored 0%) adding: goods/ (deflated 71%) adding: goods/ (deflated 74%)

2,解压时会要求输⼊密码:[root@kubemaster zip]# unzip

Archive: [] goods/ password:

五,例⼦:zip包加备注 :1,添加备注:[root@kubemaster zip]# zip -z er new zip file comment (end with .):goods list zipimportant files..

2,查看zip⽂件中已添加的注释[root@kubemaster zip]# zipnote

...goods list zipimportant files.

3,⽤zipinfo也可以查看注释# -z: 打印注释[root@kubemaster zip]# zipinfo -z

Archive: ds list zipimportant file size: 2052 bytes, number of entries: 7-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 -rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 -rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 crondrwxr-xr-x 3.0 unx 0 bx stor 20-Jul-31 11:22 goods/-rw-r--r-- 3.0 unx 21 tx defN 20-Jul-31 11:22 goods/-rw-r--r-- 3.0 unx 23 tx defN 20-Jul-31 11:22 goods/7 files, 7381 bytes uncompressed, 1004 bytes compressed: 86.4%

六,unzip的常⽤例⼦1,验证压缩包是否完整

#-t:test compressed archive data[root@kubemaster zip]# unzip -t hive: testing: goods/ OK[] goods/ password:

testing: goods/ OK testing: goods/ OKNo errors detected in compressed data of . 2,解压缩到指定⽬录:#-d:指定要解压到的⽬录[root@kubemaster zip]# unzip -d /root/unzipArchive: creating: /root/unzip/goods/[] goods/ password:

inflating: /root/unzip/goods/

inflating: /root/unzip/goods/

[root@kubemaster zip]# ls /root/unzip/goods

3,解压缩时取消⽬录:#-j:忽略⽂件原有⽬录,把⽂件统⼀解压到第⼀级⽬录[root@kubemaster zip]# unzip -j -d /root/undirArchive: [] goods/ password:

inflating: /root/undir/

inflating: /root/undir/

[root@kubemaster zip]# ls /root/

4,解压缩指定的⽂件说明:把要解压的⽂件名写到命令⾏中zip包后⾯即可[root@kubemaster zip]# unzip goods/ -d /root/undir2Archive: [] goods/ password:

inflating: /root/undir2/goods/

[root@kubemaster zip]# ls -R /root/undir2/root/undir2:goods/root/undir2/goods:

七,查看linux的版本:[root@kubemaster ~]# cat /etc/redhat-release

CentOS Linux release 8.2.2004 (Core)