linux下zip_unzip详解

命令列表:
zip
    -q (quiet)
    -r (recursive)
    -0(level0-level9)
    -e (encrypt)
    -u (update)
    -m (move into zipfile delete original-Source files)
    
unzip
    -d (directory)
    -P (password)
    -x (exclude)
    -o (overwrite)
    -n (never overwrite)
    -l (list archive)
    -t (test compressed archive data)
    -v (list verbosely/show version info)
    -j (junk paths "do not make directories")
    
扩展:    
zipcloak
zipdetails
zipgrep
zipinfo
zipsplit

环境
[root@osker ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@osker ~]# uname -r
3.10.0-957.el7.x86_64

安装zip、unzip
[root@osker ~]# yum install -y zip unzip
[root@osker ~]# rpm -qa zip unzip
zip-3.0-11.el7.x86_64
unzip-6.0-20.el7.x86_64

zip

如何使用zip压缩一个或多个文件?
[root@osker ~]# touch test{1..5}.txt
[root@osker ~]# ll
total 4
-rw-------. 1 root root 1392 Apr 19  2019 anaconda-ks.cfg
-rw-r--r--  1 root root    0 Apr 10 20:10 test1.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test2.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test3.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test4.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test5.txt
#test为压缩后的文件名,后面跟需要压缩的文件。
[root@osker ~]# zip test test1.txt test2.txt
  adding: test1.txt (stored 0%)
  adding: test2.txt (stored 0%)
[root@osker ~]# ll
total 8
-rw-------. 1 root root 1392 Apr 19  2019 anaconda-ks.cfg
-rw-r--r--  1 root root    0 Apr 10 20:10 test1.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test2.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test3.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test4.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test5.txt
-rw-r--r--  1 root root  314 Apr 10 20:11 test.zip
[root@osker ~]# unzip -l test.zip
Archive:  test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  04-10-2020 20:10   test1.txt
        0  04-10-2020 20:10   test2.txt
---------                     -------
        0                     2 files
[root@osker ~]# file test.zip
test.zip: Zip archive data, at least v1.0 to extract
        
以上过程可以看出,zip压缩后,创建的压缩包自动添加.zip

-q (quiet)
如果不想压缩的时候显示信息使用-q (quiet) 参数
[root@osker ~]# zip -q archive.zip test1.txt test2.txt test3.txt
[root@osker ~]# ll
total 12
-rw-------. 1 root root 1392 Apr 19  2019 anaconda-ks.cfg
-rw-r--r--  1 root root  460 Apr 10 20:17 archive.zip
-rw-r--r--  1 root root    0 Apr 10 20:10 test1.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test2.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test3.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test4.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test5.txt
-rw-r--r--  1 root root  314 Apr 10 20:11 test.zip

-r (recursive)
如果要压缩一个目录,就需要用-r (recursive) 参数
[root@osker ~]# mkdir dir
[root@osker ~]# mv test{1..5}.txt dir/
[root@osker ~]# ll dir
total 0
-rw-r--r-- 1 root root 0 Apr 10 20:10 test1.txt
-rw-r--r-- 1 root root 0 Apr 10 20:10 test2.txt
-rw-r--r-- 1 root root 0 Apr 10 20:10 test3.txt
-rw-r--r-- 1 root root 0 Apr 10 20:10 test4.txt
-rw-r--r-- 1 root root 0 Apr 10 20:10 test5.txt
[root@osker ~]# zip -r dir.zip dir
  adding: dir/ (stored 0%)
  adding: dir/test1.txt (stored 0%)
  adding: dir/test2.txt (stored 0%)
  adding: dir/test3.txt (stored 0%)
  adding: dir/test4.txt (stored 0%)
  adding: dir/test5.txt (stored 0%)
[root@osker ~]# ll
total 16
-rw-------. 1 root root 1392 Apr 19  2019 anaconda-ks.cfg
-rw-r--r--  1 root root  460 Apr 10 20:17 archive.zip
drwxr-xr-x  2 root root   91 Apr 10 20:20 dir
-rw-r--r--  1 root root  928 Apr 10 20:21 dir.zip
-rw-r--r--  1 root root  314 Apr 10 20:11 test.zip
[root@osker ~]# unzip -l dir.zip
Archive:  dir.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  04-10-2020 20:20   dir/
        0  04-10-2020 20:10   dir/test1.txt
        0  04-10-2020 20:10   dir/test2.txt
        0  04-10-2020 20:10   dir/test3.txt
        0  04-10-2020 20:10   dir/test4.txt
        0  04-10-2020 20:10   dir/test5.txt
---------                     -------
        0                     6 files

-(0-9)        
设置压缩级别,默认压缩级别为-6,-0为只打包不压缩,-9压缩率最高,花费时间久。
[root@osker ~]# zip -6 -r 6dir.zip dir/
  adding: dir/ (stored 0%)
  adding: dir/test1.txt (stored 0%)
  adding: dir/test2.txt (stored 0%)
  adding: dir/test3.txt (stored 0%)
  adding: dir/test4.txt (stored 0%)
  adding: dir/test5.txt (stored 0%)
[root@osker ~]# zip -r xdir.zip dir/
  adding: dir/ (stored 0%)
  adding: dir/test1.txt (stored 0%)
  adding: dir/test2.txt (stored 0%)
  adding: dir/test3.txt (stored 0%)
  adding: dir/test4.txt (stored 0%)
  adding: dir/test5.txt (stored 0%)
[root@osker ~]# ll 6dir.zip xdir.zip
-rw-r--r-- 1 root root 928 Apr 10 20:27 6dir.zip
-rw-r--r-- 1 root root 928 Apr 10 20:28 xdir.zip
可以看出默认为-6压缩级别

-e (encrypt)
为压缩包添加密码,使用-e (encrypt) 参数
[root@osker ~]# zip -e -q edir.zip dir/test1.txt
Enter password:
Verify password:
[root@osker ~]# ll edir.zip
-rw-r--r-- 1 root root 204 Apr 10 20:31 edir.zip
[root@osker ~]# unzip edir.zip
Archive:  edir.zip
[edir.zip] dir/test1.txt password:
replace dir/test1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: dir/test1.txt

-u (update): only changed or new files
需要追加文件到压缩包时,需要使用-u参数
[root@osker ~]# zip an.zip -u test1.txt
  adding: test1.txt (stored 0%)

-m (move into zipfile delete original-Source files)  
[root@osker ~]# zip an.zip -u test1.txt
  adding: test1.txt (stored 0%)
[root@osker ~]# ll
total 8
-rw-------. 1 root root 1392 Apr 19  2019 anaconda-ks.cfg
-rw-r--r--  1 root root 1080 Apr 10 21:33 an.zip
-rw-r--r--  1 root root    0 Apr 10 21:32 test1.txt
[root@osker ~]# zip -m test.zip test1.txt
  adding: test1.txt (stored 0%)
[root@osker ~]# ll
total 12
-rw-------. 1 root root 1392 Apr 19  2019 anaconda-ks.cfg
-rw-r--r--  1 root root 1080 Apr 10 21:33 an.zip
-rw-r--r--  1 root root  168 Apr 10 21:37 test.zip
可以发现压缩后test1.txt文件没有了

unzip

解压缩,默认解压到当前目录
[root@osker ~]# unzip test.zip
Archive:  test.zip
 extracting: test1.txt               
 extracting: test2.txt               
[root@osker ~]# ll
total 12
-rw-------. 1 root root 1392 Apr 19  2019 anaconda-ks.cfg
drwxr-xr-x  2 root root   91 Apr 10 20:31 dir
-rw-r--r--  1 root root  204 Apr 10 20:31 edir.zip
-rw-r--r--  1 root root    0 Apr 10 20:10 test1.txt
-rw-r--r--  1 root root    0 Apr 10 20:10 test2.txt
-rw-r--r--  1 root root  314 Apr 10 20:11 test.zip

-d (directory)
解压到指定目录,使用-d (directory) 参数
[root@osker ~]# unzip test.zip -d /opt
Archive:  test.zip
   creating: /opt/dir/
 extracting: /opt/dir/test2.txt      
 extracting: /opt/dir/test3.txt      
 extracting: /opt/dir/test4.txt      
 extracting: /opt/dir/test5.txt      
 extracting: /opt/dir/test1.txt      
[root@osker ~]# ll /opt/dir/
total 0
-rw-r--r-- 1 root root 0 Apr 10 20:10 test1.txt
-rw-r--r-- 1 root root 0 Apr 10 20:10 test2.txt
-rw-r--r-- 1 root root 0 Apr 10 20:10 test3.txt
-rw-r--r-- 1 root root 0 Apr 10 20:10 test4.txt
-rw-r--r-- 1 root root 0 Apr 10 20:10 test5.txt

-P (password)
解压加密的压缩包时,不想使用交互式,可以使用-P (password)参数
[root@osker ~]# unzip -P 1 edir.zip      #密码为1
Archive:  edir.zip
 extracting: dir/test1.txt

-x (exclude)
解压时候,需要排除某个文件不解压,使用-x (exclude)参数
[root@osker ~]# unzip test.zip -x dir/test1.txt
Archive:  test.zip
   creating: dir/
 extracting: dir/test2.txt           
 extracting: dir/test3.txt           
 extracting: dir/test4.txt           
 extracting: dir/test5.txt

-o (overwrite)
当解压的文件提示需要覆盖确认的时候,可以使用-o参数,不提示直接覆盖。
[root@osker ~]# unzip test.zip
Archive:  test.zip
replace dir/test2.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
 extracting: dir/test2.txt           
 extracting: dir/test3.txt           
 extracting: dir/test4.txt           
 extracting: dir/test5.txt           
 extracting: dir/test1.txt           
[root@osker ~]# unzip -o test.zip
Archive:  test.zip
 extracting: dir/test2.txt           
 extracting: dir/test3.txt           
 extracting: dir/test4.txt           
 extracting: dir/test5.txt           
 extracting: dir/test1.txt

-n (never overwrite)
解压目标目录中没有的文件
[root@osker ~]# unzip -n test.zip
Archive:  test.zip
 extracting: dir/new.txt
 
 
-l (list archive)
 不解压查看压缩包使用-l参数
 [root@osker ~]# unzip -l edir.zip
Archive:  edir.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  04-10-2020 20:10   dir/test1.txt
---------                     -------
        0                     1 file

-t (test compressed archive data)
测试压缩包有无损坏
[root@osker ~]# unzip -t an.zip
Archive:  an.zip
    testing: anaconda-ks.cfg          OK
    testing: test1.txt                OK
No errors detected in compressed data of an.zip.        
        
        
-v (list verbosely/show version info)
列出压缩包里的详细信息
[root@osker ~]# unzip -v an.zip
Archive:  an.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
    1392  Defl:N      754  46% 04-19-2019 12:22 60a52de9  anaconda-ks.cfg
       0  Stored        0   0% 04-10-2020 21:32 00000000  test1.txt
--------          -------  ---                            -------
    1392              754  46%                            2 files
[root@osker ~]# ll
total 12
-rw-------. 1 root root 1392 Apr 19  2019 anaconda-ks.cfg
-rw-r--r--  1 root root 1080 Apr 10 21:33 an.zip
-rw-r--r--  1 root root  168 Apr 10 21:37 test.zip

-j (junk paths "do not make directories")
解压时候不生成目录
[root@osker ~]# unzip -l dir.zip
Archive:  dir.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  04-10-2020 21:47   dir/
        0  04-10-2020 21:47   dir/test1.txt
        0  04-10-2020 21:47   dir/test2.txt
        0  04-10-2020 21:47   dir/test3.txt
---------                     -------
        0                     4 files
[root@osker ~]# unzip -j dir.zip
Archive:  dir.zip
 extracting: test1.txt               
 extracting: test2.txt               
 extracting: test3.txt               
[root@osker ~]# ll
total 8
-rw-------. 1 root root 1392 Apr 19  2019 anaconda-ks.cfg
drwxr-xr-x  2 root root   57 Apr 10 21:47 dir
-rw-r--r--  1 root root  620 Apr 10 21:47 dir.zip
-rw-r--r--  1 root root    0 Apr 10 21:47 test1.txt
-rw-r--r--  1 root root    0 Apr 10 21:47 test2.txt
-rw-r--r--  1 root root    0 Apr 10 21:47 test3.txt        
        
        
        
        
zipcloak
有时候压缩文件的时候忘记添加密码,可以使用zipcloak命令追加密码。
[root@osker ~]# zipcloak test.zip
Enter password:
Verify password:
encrypting: dir/
encrypting: dir/test2.txt
encrypting: dir/test3.txt
encrypting: dir/test4.txt
encrypting: dir/test5.txt
encrypting: dir/test1.txt
encrypting: dir/new.txt

zipdetails
可能需要安装此包
[root@osker ~]# yum install -y perl-IO-Compress
查看zip包详细属性
[root@osker ~]# zipdetails an.zip
0000 LOCAL HEADER #1       04034B50
0004 Extract Zip Spec      14 '2.0'
0005 Extract OS            00 'MS-DOS'
0006 General Purpose Flag  0000
     [Bits 1-2]            0 'Normal Compression'
0008 Compression Method    0008 'Deflated'
000A Last Mod Time         4E9362D7 'Fri Apr 19 12:22:46 2019'
000E CRC                   60A52DE9
0012 Compressed Length     000002F2
0016 Uncompressed Length   00000570
001A Filename Length       000F
001C Extra Length          001C
001E Filename              'anaconda-ks.cfg'
......

zipgrep
过滤压缩包的内容
[root@osker ~]# zipgrep root  an.zip
anaconda-ks.cfg:rootpw --iscrypted $6$4MbbflESaQBP8rLd$zgLUIfBs9zgnNkeDfA5G67PKjt7rdf.Z/ChB59kgYw.7804KH2hiXCL1SXcCjaMrHceZcRvSN2yVcst/D/qGL/
anaconda-ks.cfg:pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty          
[root@osker ~]# grep 'root' anaconda-ks.cfg
rootpw --iscrypted $6$4MbbflESaQBP8rLd$zgLUIfBs9zgnNkeDfA5G67PKjt7rdf.Z/ChB59kgYw.7804KH2hiXCL1SXcCjaMrHceZcRvSN2yVcst/D/qGL/
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
可以看到和grep出来的结果是一样的。

zipinfo
查看压缩包里文件的详细属性
[root@osker ~]# zipinfo test.zip
Archive:  test.zip
Zip file size: 1162 bytes, number of entries: 7
drwxr-xr-x  3.0 unx        0 Bx stor 20-Apr-10 20:58 dir/
-rw-r--r--  3.0 unx        0 Bx stor 20-Apr-10 20:10 dir/test2.txt
-rw-r--r--  3.0 unx        0 Bx stor 20-Apr-10 20:10 dir/test3.txt
-rw-r--r--  3.0 unx        0 Bx stor 20-Apr-10 20:10 dir/test4.txt
-rw-r--r--  3.0 unx        0 Bx stor 20-Apr-10 20:10 dir/test5.txt
-rw-r--r--  3.0 unx        0 Bx stor 20-Apr-10 20:10 dir/test1.txt
-rw-r--r--  3.0 unx        0 Bx stor 20-Apr-10 20:58 dir/new.txt
7 files, 0 bytes uncompressed, 0 bytes compressed:  0.0%

zipsplit
将压缩包切分为多个小包
[root@osker ~]# ll -h
-rw-r--r--  1 root root 1.2K Apr 10 21:03 test.zip
[root@osker ~]# zipsplit -n 1024 test.zip
2 zip files will be made (100% efficiency)
creating: test1.zip
creating: test2.zip
[root@osker ~]# ll test*
-rw-r--r-- 1 root root 1000 Apr 10 21:16 test1.zip
-rw-r--r-- 1 root root  184 Apr 10 21:16 test2.zip
-rw-r--r-- 1 root root 1162 Apr 10 21:03 test.zip

linux下zip/unzip详解的更多相关文章

  1. Linux下ps命令详解 Linux下ps命令的详细使用方法

    http://www.jb51.net/LINUXjishu/56578.html Linux下的ps命令比较常用 Linux下ps命令详解Linux上进程有5种状态:1. 运行(正在运行或在运行队列 ...

  2. Linux下rar命令详解

    Linux下rar命令详解 用法: rar <命令> -<选项1> ….-<选项N> < 操作文档> <文件…> <@文件列表…> ...

  3. linux下tar命令详解

     linux下tar命令详解    tar是Linux环境下最常用的备份工具之一.tar(tap archive)原意为操作磁带文件,但基于Linux的文件操作机制,同样也可适用于普通的磁盘文件.ta ...

  4. Linux下chkconfig命令详解(转)

    Linux下chkconfig命令详解 chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. ...

  5. Linux知识积累(4) Linux下chkconfig命令详解

    Linux下chkconfig命令详解 chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. ...

  6. Linux下top命令详解

    Linux下top命令详解 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.top是一个动态显示过程,即可以通过用户按键来不断刷 ...

  7. 转载的 Linux下chkconfig命令详解

    Linux下chkconfig命令详解 chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. ...

  8. Linux下桥接模式详解一

    注册博客园已经好长时间,一直以来也没有在上面写过文章,都是随意的记录在了未知笔记上,今天开始本着分享和学习的精神想把之前总结的笔记逐步分享到博客园,和大家一起学习,一起进步吧! 2016-09-20  ...

  9. linux下IPTABLES配置详解 (防火墙命令)

    linux下IPTABLES配置详解 -A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 24000 -j ACCEPT ...

随机推荐

  1. jquery 的animate 的transform

    $(function(){ var t = 1000; $("#id").animate( {borderSpacing:180}, //180 指旋转度数 { step: fun ...

  2. Python 解密JWT验证苹果登录

    验证苹果登录,官方提供两种验证方法,一种是token,另一个种是code.这里使用的是token 登录流程: 苹果客户端调用苹果API,获取到用户的信息,包括: user_id 昵称 identity ...

  3. 通过nodejs实现文件的上传

    通过nodejs实现文件的上传 主要内容 本文将用来讲述如何通过nodejs进行文件上传,将会涉及到以下知识点: 通过express模块进行服务器的搭建 通过multer模块将上传的文件保存到指定目录 ...

  4. jmeter 性能测试基本过程及示例

    jmeter 为性能测试提供了一下特色: jmeter 可以对测试静态资源(例如 js.html 等)以及动态资源(例如 php.jsp.ajax 等等)进行性能测试jmeter 可以挖掘出系统最大能 ...

  5. pc 媒体查询

    PC端 按屏幕宽度大小排序(主流的用橙色标明) 分辨率   比例 | 设备尺寸 1024*500 (8.9寸) 1024*768 (比例4:3  | 10.4寸.12.1寸.14.1寸.15寸; ) ...

  6. jsp(3,6,9) EL表达式及JSTL

    1. jsp 1.1jsp是什么 全称: Java Server Pages,java服务器页面.和Servlet一样,是sun公司定义的一种动态网页开发技术.    特点:基于html模版,可以在h ...

  7. asp.net net::ERR_ABORTED 500 (Internal Server Error) 无法加载JS CSS等文件的解决方法

    网站换服务器,部署上去后打开首页,js .css等文件始终无法加载,经过排查,问题出现在web.config文件中. <defaultDocument> <files> < ...

  8. Matplotlib 精简实例入门

    Matplotlob 简明实例入门 通过几个实例,快速了解matplotlib.pyplot 中最为常见的折线图,散点图,柱状图,直方图,饼图的用法 如果您需要更为详细的内容,请参考官方文档: htt ...

  9. http服务部署

    [root@xiaoyi ~]# vim /etc/ssh/sshd_config 17行 Port 22 49行 PermitRootLogin yes 观察IP地址与物理机属于同一个网段 打开Fi ...

  10. 【bzoj2049】[Sdoi2008]Cave 洞穴勘测——线段树上bfs求可撤销并查集

    题面 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 12030 Solved: 6024 Desc ...