一,查看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

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

对应的源码可以访问这里获取: https://github.com/liuhongdi/

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

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

1,zip
直接输入命令后会打印版本和帮助信息
[root@kubemaster ~]# zip
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 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 standard input.
...
2,unzip:
直接输入命令后会打印版本和帮助信息
[root@kubemaster ~]# unzip
UnZip 6.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 files in list, except those in xlist, to exdir;
...

三,zip的常用例子

1,把文件打包压缩进zip文件
[root@kubemaster zip]# zip t1.zip a.txt b.txt
adding: a.txt (deflated 82%)
adding: b.txt (deflated 82%)

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

用zip查看

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

[root@kubemaster zip]# zip -sf t1.zip
Archive contains:
a.txt
b.txt
Total 2 entries (112 bytes)

用unzip查看

#-l:列出文件

[root@kubemaster zip]# unzip -l t1.zip
Archive: t1.zip
Length Date Time Name
--------- ---------- ----- ----
56 07-28-2020 18:38 a.txt
56 07-28-2020 18:38 b.txt
--------- -------
112 2 files

用zipinfo查看:

[root@kubemaster zip]# zipinfo t1.zip
Archive: t1.zip
Zip file size: 318 bytes, number of entries: 2
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 a.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 b.txt
2 files, 112 bytes uncompressed, 20 bytes compressed: 82.1%

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

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

[root@kubemaster zip]# zip t1.zip c.txt
adding: c.txt (deflated 82%)
[root@kubemaster zip]# zipinfo t1.zip
Archive: t1.zip
Zip file size: 466 bytes, number of entries: 3
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 a.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 b.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 c.txt
3 files, 168 bytes uncompressed, 30 bytes compressed: 82.1%

可以看到已添加成功

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

[root@kubemaster zip]# zip -d t1.zip c.txt
deleting: c.txt
[root@kubemaster zip]# zipinfo t1.zip
Archive: t1.zip
Zip file size: 318 bytes, number of entries: 2
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 a.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 b.txt
2 files, 112 bytes uncompressed, 20 bytes compressed: 82.1%

可以看到已删除成功

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

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

[root@kubemaster zip]# zip -j t1.zip /var/log/cron
adding: cron (deflated 86%)
[root@kubemaster zip]# zipinfo t1.zip
Archive: t1.zip
Zip file size: 1559 bytes, number of entries: 4
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 a.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 b.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 c.txt
-rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron
4 files, 7141 bytes uncompressed, 987 bytes compressed: 86.2%

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

6,替换zip包中的文件:

[root@kubemaster zip]# zipinfo t1.zip
Archive: t1.zip
Zip file size: 1559 bytes, number of entries: 4
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 a.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 b.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 c.txt
-rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron
4 files, 7141 bytes uncompressed, 987 bytes compressed: 86.2%

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

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

[root@kubemaster zip]# zip -u t1.zip a.txt
[root@kubemaster zip]# zipinfo t1.zip
Archive: t1.zip
Zip file size: 1564 bytes, number of entries: 4
-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25 a.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 b.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 c.txt
-rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron
4 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]# ls
ga.txt gb.txt
[root@kubemaster goods]# cd ..
[root@kubemaster zip]# zip -r t1.zip ./goods/
adding: goods/ (stored 0%)
adding: goods/ga.txt (deflated 71%)
adding: goods/gb.txt (deflated 74%)
[root@kubemaster zip]# zipinfo t1.zip
Archive: t1.zip
Zip file size: 2020 bytes, number of entries: 7
-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25 a.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 b.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 c.txt
-rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron
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.txt
7 files, 7381 bytes uncompressed, 1004 bytes compressed: 86.4%

四,例子:zip包加密码

1,加密码:
#-P:指定添加文件的密码,解压此文件时会要求输入
注意:是此命令行中所添加的文件有密码,不是给整个zip包加的密码
多个文件可以分别对应多个不同的密码
[root@kubemaster zip]# zip -P pass -r t2.zip goods
adding: goods/ (stored 0%)
adding: goods/ga.txt (deflated 71%)
adding: goods/gb.txt (deflated 74%)
2,解压时会要求输入密码:
[root@kubemaster zip]# unzip t2.zip
Archive: t2.zip
[t2.zip] goods/ga.txt password:

五,例子:zip包加备注 :

1,添加备注:

[root@kubemaster zip]# zip -z t1.zip
enter new zip file comment (end with .):
goods list zip
important files.
.

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

[root@kubemaster zip]# zipnote t1.zip
...
goods list zip
important files.

3,用zipinfo也可以查看注释

# -z: 打印注释

[root@kubemaster zip]# zipinfo -z t1.zip
Archive: t1.zip
goods list zip
important files.
Zip file size: 2052 bytes, number of entries: 7
-rw-r--r-- 3.0 unx 252 tx defN 20-Jul-28 19:25 a.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 18:38 b.txt
-rw-r--r-- 3.0 unx 56 tx defN 20-Jul-28 19:02 c.txt
-rw------- 3.0 unx 6973 tx defN 20-Jul-28 19:01 cron
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.txt
7 files, 7381 bytes uncompressed, 1004 bytes compressed: 86.4%

六,unzip的常用例子

1,验证压缩包是否完整 
#-t:test compressed archive data
[root@kubemaster zip]# unzip -t t2.zip
Archive: t2.zip
testing: goods/ OK
[t2.zip] goods/ga.txt password:
testing: goods/ga.txt OK
testing: goods/gb.txt OK
No errors detected in compressed data of t2.zip.
2,解压缩到指定目录:
#-d:指定要解压到的目录
[root@kubemaster zip]# unzip t2.zip -d /root/unzip
Archive: t2.zip
creating: /root/unzip/goods/
[t2.zip] goods/ga.txt password:
inflating: /root/unzip/goods/ga.txt
inflating: /root/unzip/goods/gb.txt
[root@kubemaster zip]# 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@kubemaster zip]# 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@kubemaster zip]# ls -R /root/undir2
/root/undir2:
goods /root/undir2/goods:
gb.txt

七,查看linux的版本:

[root@kubemaster ~]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)

linux(centos8):使用zip/unzip压缩和解压缩文件的更多相关文章

  1. Linux常用命令学习3---(文件的压缩和解压缩命令zip unzip tar、关机和重启命令shutdown reboot……)

    1.压缩和解压缩命令    常用压缩格式:.zip..gz..bz2..tar.gz..tar.bz2..rar .zip格式压缩和解压缩命令        zip 压缩文件名 源文件:压缩文件   ...

  2. 【转载】 Linux常用命令: zip、unzip 压缩和解压缩命令

    Linux常用命令: zip.unzip 压缩和解压缩命令   Linux常用命令: zip.unzip 压缩和解压缩命令 zip的用法 基本用法是: zip [参数] [打包后的文件名] [打包的目 ...

  3. 【转载】linux 压缩和解压缩命令gz、tar、zip、bz2

    linux系统下压缩解压缩很让人头大,每次都要查命令.转载下方便以后查阅.原文信息如下: 作者:capecape 来源:CSDN 原文:https://blog.csdn.net/capecape/a ...

  4. Linux系列教程(八)——Linux常用命令之压缩和解压缩命令

    前面一篇博客我们讲解了Linux帮助和用户管理命令,对于帮助命令,man 命令能获得命令和配置文件的帮助信息,help命令能获得shell内置命令的帮助信息.我们可以通过which来区分什么是shel ...

  5. Linux常用命令之压缩和解压缩命令

    目录 1.压缩解压缩格式 .gz 一.将文件压缩为 .gz 格式,只能压缩文件:gzip 二.将 .gz 文件解压:gunzip 2.压缩解压缩格式 .tar.gz 一.将文件或目录压缩为 .tar. ...

  6. Linux命令——压缩和解压缩

    Linux命令--压缩和解压缩 尽管文件后缀名在Linux中没什么用,但还是来看看: .gz:表示由gzip压缩工具压缩的文件 .bz2:表示由bzip2压缩工具压缩的文件 .tar:表示由tar打包 ...

  7. Linux关于压缩和解压缩实例

    在谈到压缩和解压缩,我想说说它们的应用场景,其实它们主要的应用场景是有这么几个方面? (1)备份(几十个数据库每天进行备份,即包含数据又包含脚本,还有其他十分重要的日志文件等等); (2)降低服务器存 ...

  8. 『学了就忘』Linux基础命令 — 32、压缩和解压缩相关命令

    目录 1.".zip"格式压缩 2.".gz"格式压缩 3.".bz2"格式压缩 4.".tar"格式打包 5.打包和压 ...

  9. Linux下的压缩和解压缩命令——gzip/gunzip

    gzip命令 gzip命令用来压缩文件.gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多处".gz"扩展名. gzip是在Linux系统中经常使用的一个对文件进行压 ...

随机推荐

  1. Sql Server中使用特定字符分割字符串

    在T-SQL中我们经常批量操作时都会对字符串进行拆分,可是SQL Server中却没有自带Split函数,所以要自己来实现了.这里将字符串分割以table形式输出 语法如下: SET ANSI_NUL ...

  2. java执行器

    Executor 执行已提交的 Runnable 任务对象.此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节.调度等)分离开来的方法.Executor 接口并没有严格地要求执行是 ...

  3. vue中axios的使用

    新开了一个vue项目,从头到尾都是一个人做的,所以就自己配置了一个axios.js文件 第一种方法.使用axios 需要下载安装   npm install axios,下载完成之后 在main.js ...

  4. Shell学习(二)Shell变量

    一.Shell变量 变量的定义 例子: my_job="Learn Shell" PS:变量名和等号之间不能有空格!!! 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头 ...

  5. Spring学习(六)bean装配详解之 【通过注解装配 Bean】【基础配置方式】

    通过注解装配 Bean 1.前言 优势 1.可以减少 XML 的配置,当配置项多的时候,XML配置过多会导致项目臃肿难以维护 2.功能更加强大,既能实现 XML 的功能,也提供了自动装配的功能,采用了 ...

  6. zabbix_server.conf配置文件参数

    NodeID: 在amster-child 的分布式架构中,这个ID是唯一标识zabbix node的号码 ListenPort:Trapper 类型Item监听的端口, SourceIP: 在连接其 ...

  7. JavaScript,你好!(二)

    操作BOM对象 浏览器介绍 JavaScript和浏览器的关系? JavaScrpit诞生就是为了能够让它在浏览器中运行! BOM:浏览器对象模型 IE 6~11 Chrome Safari Fire ...

  8. Unity Shader学习笔记-1

    本篇文章是对Unity Shader入门精要的学习笔记,插图大部分来自冯乐乐女神的github 如果有什么说的不正确的请批评指正 目录 渲染流水线 流程图 Shader作用 屏幕映射 三角形遍历 两大 ...

  9. 高并发场景-请求合并(二)揭秘HystrixCollapser-利用Queue和线程池异步实现

    背景 在互联网的高并发场景下,请求会非常多,但是数据库连接池比较少,或者说需要减少CPU压力,减少处理逻辑的,需要把单个查询,用某些手段,改为批量查询多个后返回. 如:支付宝中,查询"个人信 ...

  10. mysql 空值(null)和空字符('')的区别

    日常开发中,一般都会涉及到数据库增删改查,那么不可避免会遇到Mysql中的NULL和空字符. 空字符(")和空值(null)表面上看都是空,其实存在一些差异: 定义: 空值(NULL)的长度 ...