linux(centos8):使用zip/unzip壓縮和解壓縮文件


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM