当前位置: 首页 > 工具软件 > Info-ZIP > 使用案例 >

linux中解压文件到root,linux(centos8):使用zip/unzip压缩和解压缩文件

臧翰采
2023-12-01

一,查看zip命令所属的rpm包

1,zip

[root@kubemaster ~]# whereis zip

zip: /usr/bin/zip /usr/share/man/man1/zip.1.gz

[root@kubemaster~]# rpm -qf /usr/bin/zip

zip-3.0-23.el8.x86_64

如果找不到zip命令,

可以用dnf进行安装

[root@kubemaster ~]# dnf install zip

2,unzip

[root@kubemaster ~]# whereis unzip

unzip: /usr/bin/unzip /usr/share/man/man1/unzip.1.gz

[root@kubemaster~]# rpm -qf /usr/bin/unzip

unzip-6.0-43.el8.x86_64

如果找不到unzip命令,

可以用dnf进行安装

[root@kubemaster ~]# dnf install unzip

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

二,查看zip命令的版本和帮助:

1,zip

直接输入命令后会打印版本和帮助信息

[root@kubemaster ~]# zipCopyright (c)1990-2008 Info-ZIP - Type 'zip "-L"' forsoftware license.

Zip3.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,whichcan include the special name-to compress standard input.

...

2,unzip:

直接输入命令后会打印版本和帮助信息

[root@kubemaster ~]# unzipUnZip6.00 of 20 April 2009, by Info-ZIP. Maintained by C. Spieler. Send

bug reports using http://www.info-zip.org/zip-bug.html; see README for details.

Usage:unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]

Default action is to extract filesin list, except those in xlist, to exdir;

...

三,zip的常用例子

1,把文件打包压缩进zip文件

[root@kubemaster zip]# zip t1.zipa.txt b.txt

adding: a.txt (deflated82%)

adding: b.txt (deflated82%)

2,查看zip包中包含的文件:

用zip查看

#-sf:--show-files:显示文件列表

[root@kubemaster zip]# zip -sf t1.zipArchive contains:

a.txt

b.txt

Total2 entries (112 bytes)

用unzip查看

#-l:列出文件

[root@kubemaster zip]# unzip -l t1.zipArchive: t1.zipLength Date Time Name--------- ---------- ----- ----

56 07-28-2020 18:38a.txt56 07-28-2020 18:38b.txt--------- -------

112 2 files

用zipinfo查看:

[root@kubemaster zip]# zipinfo t1.zipArchive: t1.zipZipfile size: 318 bytes, number of entries: 2

-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38a.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38b.txt2 files, 112 bytes uncompressed, 20 bytes compressed: 82.1%

说明:zipinfo是unzip包中自带的命令

3,添加一个文件到现有的zip包

[root@kubemaster zip]# zip t1.zipc.txt

adding: c.txt (deflated82%)

[root@kubemasterzip]# zipinfo t1.zipArchive: t1.zipZipfile size: 466 bytes, number of entries: 3

-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38a.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38b.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02c.txt3 files, 168 bytes uncompressed, 30 bytes compressed: 82.1%

可以看到已添加成功

4,从现有的zip压缩包中删除一个文件

[root@kubemaster zip]# zip -d t1.zipc.txt

deleting: c.txt

[root@kubemasterzip]# zipinfo t1.zipArchive: t1.zipZipfile size: 318 bytes, number of entries: 2

-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38a.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38b.txt2 files, 112 bytes uncompressed, 20 bytes compressed: 82.1%

可以看到已删除成功

5,添加到zip时去掉原目录,只保留文件名:

#-j:不处理压缩文件中原有的目录路径

[root@kubemaster zip]# zip -j t1.zip /var/log/cron

adding: cron (deflated86%)

[root@kubemasterzip]# zipinfo t1.zipArchive: t1.zipZipfile size: 1559 bytes, number of entries: 4

-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38a.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38b.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02c.txt-rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01cron4 files, 7141 bytes uncompressed, 987 bytes compressed: 86.2%

可以看到cron已经被去掉了目录信息

6,替换zip包中的文件:

[root@kubemaster zip]# zipinfo t1.zipArchive: t1.zipZipfile size: 1559 bytes, number of entries: 4

-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38a.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38b.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02c.txt-rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01cron4 files, 7141 bytes uncompressed, 987 bytes compressed: 86.2%

可以看到a.txt的文件大小是56

我们修改a.txt文件后,替换压缩包中的此文件

[root@kubemaster zip]# zip -u t1.zipa.txt

[root@kubemasterzip]# zipinfo t1.zipArchive: t1.zipZipfile size: 1564 bytes, number of entries: 4

-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25a.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38b.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02c.txt-rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01cron4 files, 7337 bytes uncompressed, 992 bytes compressed: 86.5%

已替换成功

7,指定压缩比时,默认的压缩比是多少?

[root@kubemaster zip]# zip -h2

...-0store files (no compression)-1 to -9 compress fastest to compress best (default is 6)

...

这个值一般不需要改动

8,压缩一个目录:

# -r :recurse into directories递归压缩目录

[root@kubemaster goods]# lsga.txt gb.txt

[root@kubemaster goods]# cd ..

[root@kubemasterzip]# zip -r t1.zip ./goods/adding: goods/ (stored 0%)

adding: goods/ga.txt (deflated 71%)

adding: goods/gb.txt (deflated 74%)

[root@kubemasterzip]# zipinfo t1.zipArchive: t1.zipZipfile size: 2020 bytes, number of entries: 7

-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25a.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38b.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02c.txt-rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01cron

drwxr-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/ga.txt-rw-r--r-- 3.0 unx 23 tx defN 20-Jul-31 11:22 goods/gb.txt7 files, 7381 bytes uncompressed, 1004 bytes compressed: 86.4%

四,例子:zip包加密码

1,加密码:

#-P:指定添加文件的密码,解压此文件时会要求输入

注意:是此命令行中所添加的文件有密码,不是给整个zip包加的密码

多个文件可以分别对应多个不同的密码

[root@kubemaster zip]# zip -P pass -r t2.zipgoods

adding: goods/ (stored 0%)

adding: goods/ga.txt (deflated 71%)

adding: goods/gb.txt (deflated 74%)

2,解压时会要求输入密码:

[root@kubemaster zip]# unzip t2.zipArchive: t2.zip[t2.zip] goods/ga.txt password:

五,例子:zip包加备注 :

1,添加备注:

[root@kubemaster zip]# zip -z t1.zipenter newzip filecomment (end with .):

goods listzipimportant files.

.

2,查看zip文件中已添加的注释

[root@kubemaster zip]# zipnote t1.zip...

goods listzipimportant files.

3,用zipinfo也可以查看注释

# -z: 打印注释

[root@kubemaster zip]# zipinfo -z t1.zipArchive: t1.zipgoods listzipimportant files.

Zipfile size: 2052 bytes, number of entries: 7

-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25a.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38b.txt-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02c.txt-rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01cron

drwxr-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/ga.txt-rw-r--r-- 3.0 unx 23 tx defN 20-Jul-31 11:22 goods/gb.txt7 files, 7381 bytes uncompressed, 1004 bytes compressed: 86.4%

六,unzip的常用例子

1,验证压缩包是否完整

#-t:test compressed archive data

[root@kubemaster zip]# unzip -t t2.zipArchive: t2.ziptesting: goods/OK

[t2.zip] goods/ga.txt password:

testing: goods/ga.txt OK

testing: goods/gb.txt OK

No errors detectedin compressed data of t2.zip.

2,解压缩到指定目录:

#-d:指定要解压到的目录

[root@kubemaster zip]# unzip t2.zip -d /root/unzipArchive: t2.zipcreating:/root/unzip/goods/[t2.zip] goods/ga.txt password:

inflating:/root/unzip/goods/ga.txt

inflating:/root/unzip/goods/gb.txt

[root@kubemasterzip]# ls /root/unzip/goods

3,解压缩时取消目录:

#-j:忽略文件原有目录,把文件统一解压到第一级目录

[root@kubemaster zip]# unzip -j t2.zip -d /root/undir

Archive: t2.zip[t2.zip] goods/ga.txt password:

inflating:/root/undir/ga.txt

inflating:/root/undir/gb.txt

[root@kubemasterzip]# ls /root/undir

ga.txt gb.txt

4,解压缩指定的文件

说明:把要解压的文件名写到命令行中zip包后面即可

[root@kubemaster zip]# unzip t2.zip goods/gb.txt -d /root/undir2

Archive: t2.zip[t2.zip] goods/gb.txt password:

inflating:/root/undir2/goods/gb.txt

[root@kubemasterzip]# ls -R /root/undir2/root/undir2:

goods/root/undir2/goods:

gb.txt

七,查看linux的版本:

[root@kubemaster ~]# cat /etc/redhat-release

CentOS Linux release8.2.2004 (Core)

 类似资料: