Linux中巧用zip命令壓縮和解壓縮檔案__Linux

來源:互聯網
上載者:User
Linux中巧用zip命令壓縮和解壓縮檔案 轉自 http://www.itivy.com/linux/archive/2012/3/22/linux-zip.html

在Windows中,我們用得最多的解壓縮軟體就是WinRAR了,這個軟體對我們實現解壓縮非常方便。但是如果在Linux系統中,我們還可以嘗試用命令來實現對檔案的解壓縮,這篇文章主要介紹在Linux中用zip命令來壓縮和解壓縮檔案。zip命令非常強大,下面我們一步步來介紹。

1、  zip命令基礎

怎樣用zip壓縮多個檔案呢。

文法: zip {.zipfile-name} {file-names}

#zip var-log-files.zip /var/log/* 
adding: var/log/acpid (deflated 81%) 
adding: var/log/anaconda.log (deflated 79%) 
adding: var/log/anaconda.syslog (deflated 73%) 
adding: var/log/anaconda.xlog (deflated 82%) 
adding: var/log/audit/ (stored 0%) adding: var/log/boot.log (stored 0%) 
adding: var/log/boot.log.1 (deflated 40%) 
adding: var/log/boot.log.2 (deflated 42%) 
adding: var/log/boot.log.3 (deflated 40%) 
adding: var/log/boot.log.4 (deflated 40%)

 

如何遞迴地壓縮一個目錄及目錄下的檔案。

#zip -r var-log-dir.zip /var/log/ 
updating: var/log/ (stored 0%) 
adding: var/log/wtmp (deflated 78%) 
adding: var/log/scrollkeeper.log (deflated 94%) 
adding: var/log/rpmpkgs.3 (deflated 68%) 
adding: var/log/spooler (stored 0%) 
adding: var/log/cron.2 (deflated 90%) 
adding: var/log/spooler.1 (stored 0%) 
adding: var/log/spooler.4 (stored 0%) 
adding: var/log/httpd/ (stored 0%) 
adding: var/log/rpmpkgs.1 (deflated 68%) 
adding: var/log/anaconda.log (deflated 79%) 
adding: var/log/secure.2 (deflated 93%)

 

如何解壓a*.zip的壓縮包。

# unzip var-log.zip 
Archive: var-log.zip 
inflating: var/log/acpid

inflating:var/log/anaconda.log 
inflating: var/log/anaconda.syslog 
inflating: var/log/anaconda.xlog 
creating: var/log/audit/

 

用參數v查看解壓縮過程中的詳細資料:

# unzip -v var-log.zip 
Archive:var-log.zip 
Length     Method     Size   Ratio Date Time     CRC-32 Name 
------     ------    ----- ----- ---- ----     ------ ---- 
1916       Defl:N     369    81% 02-08-0814:27 e2ffdc0cvar/log/acpid
13546 Defl:N 2900 79% 02-02-07 14:25 34cc03a1 var/log/anaconda.log 
skip.. 
7680 Defl:N 411 95% 12-30-08 10:55 fe876ee9 var/log/wtmp.1 
40981 Defl:N 7395 82% 02-08-08 14:28 6386a95e var/log/Xorg.0.log 
--------   -------    ---    ---    ---- 
41406991   2809229    93%    56     files

 

如何在不解壓一個壓縮包的情況下看裡面的檔案呢。

#unzip -l var-log.zip 
Archive: var-log.zip 
Length     Date       Time   Name 
--------   ----       ----   ---- 
1916       02-08-08   14:27  var/log/acpid
13546      02-02-07   14:25 var/log/anaconda.log
..skip.. 
40981      02-08-08   14:28 var/log/Xorg.0.log
40981      02-08-07   14:56 var/log/Xorg.0.log.old
--------                    ------- 
41406991                    56 files

 

2、  zip命令進階用法

 

zip命令提供了十個壓縮等級:

o等級0是最低等級,只做歸檔,不壓縮 
o等級1壓縮率低,但速度很快 
o等級6是預設的壓縮等級 
o等級9的壓縮率最高,但它耗時也多,除了大檔案,我們一般推薦於用等級9

下面的例子中我分別利用等級0,等級6,等級9壓縮同樣的一個目錄,看看他們分別壓縮後的大小:

# zip var-log-files-default.zip /var/log/* 
# zip -0 var-log-files-0.zip /var/log/* 
# zip -9 var-log-files-9.zip /var/log/* 
# ls -ltr 
-rw-r--r-- 1 root root 2817248 Jan 1 13:05 
var-log-files-default.zip 
-rw-r--r-- 1 root root 41415301 Jan 1 13:05 
var-log-files-0.zip 
-rw-r--r-- 1 root root 2582610 Jan 1 13:06 
var-log-files-9.zip

 

3、zip檔案的密碼保護

使用zip命令的P選項來加密zip檔案

#zip -P mysecurepwd var-log-protected.zip /var/log/*

在shell指令碼中使用上述選項來做後台工作是個不錯的選擇。但在使用互動式命令列時,你一般不會想讓密碼在History中可見。所以,這個時候就要使用下述的e選項來設定密碼了。

# zip -e var-log-protected.zip /var/log/* 
Enter password: 
Verify password: 
updating: var/log/acpid (deflated 81%) 
updating: var/log/anaconda.log (deflated 79%)

 

當你要解壓縮一個有密碼保護的壓縮檔時,會被要求輸入密碼。

# unzip var-log-protected.zip 
Archive: var-log-protected.zip 
[var-log-protected.zip] var/log/acpid password:

4、  檢查zip檔案的完整性

有時候你想要檢查zip檔案的完整性又不想解壓它。這時可以使用如下所述的t選項

# unzip -t var-log.zip 
Archive: var-log.zip 
testing: var/log/acpid OK 
testing: var/log/anaconda.log OK 
testing: var/log/anaconda.syslog OK 
skip... 
testing: var/log/wtmp OK 
testing: var/log/wtmp.1 OK 
testing: var/log/Xorg.0.log OK 
No errors detected in compressed data of var-log.zip.

對於Linux下zip命令壓縮檔的方法就介紹完了,對於Linux下,還有個tar命令也可以實現檔案的壓縮和解壓縮,我打算下一篇來介紹。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.