zip 指令:壓縮資料夾、用 unzip 解壓縮與壓縮選項

測試環境RHEL 系列AlmaLinux 9.8root 帳號2026-11-05 確認
壓縮 · 打包指令zipunzip位置/usr/bin/zip
壓縮資料夾與用 unzip 解壓縮

zip 指令會在 Linux 上把檔案和資料夾打包成一個 zip 檔並壓縮,解壓縮時則搭配 unzip 使用。在 AlmaLinux 9 伺服器上用 zip -r 壓縮網站資料夾後,35,825 位元組縮小到 4,158 位元組,而在 Windows 建立的 zip 檔中變成亂碼的檔案名稱,用 unzip -O 就恢復正常。

以下輸出是在 AlmaLinux 9.8 伺服器上以 root 帳號於工作資料夾內執行的結果,時間為 UTC。壓縮的 site 資料夾和備忘檔都是為測試建立的範例檔案。Windows 建立的 zip 是用檔案名稱以 CP949 儲存的測試檔重現的。

1️⃣ zip 指令用法

  • ↪️ 壓縮檔案與壓縮資料夾 (-r)

zip 後面先寫要新建的 zip 檔名稱,再列出要放進去的檔案。下面的指令把 readme.txt 和 memo.txt 兩個檔案壓縮成一個 notes.zip。

zip notes.zip readme.txt memo.txt
  adding: readme.txt (deflated 92%)
  adding: memo.txt (stored 0%)

行尾的 [deflated 92%] 是比原本大小減少的比例。71 位元組的 memo.txt 那一行的 [stored 0%],表示沒有壓縮、以原本大小直接存入。

壓縮資料夾時要加上 -r。不加 -r 只寫資料夾名稱,就只會放進資料夾項目 site/,裡面的檔案不會放進去。

zip nodir.zip site
  adding: site/ (stored 0%)

要壓縮的 site 資料夾如下,由三個子資料夾和六個檔案組成。

site/
css/
style.css
js/
app.js
logs/
access.log
index.html
about.html
.htaccess

加上 -r 後,子資料夾、裡面的檔案,連以點開頭的 .htaccess 在內,十個項目全部放進去。

zip -r site.zip site
  adding: site/ (stored 0%)
  adding: site/css/ (stored 0%)
  adding: site/css/style.css (deflated 93%)
  adding: site/js/ (stored 0%)
  adding: site/js/app.js (deflated 87%)
  adding: site/logs/ (stored 0%)
  adding: site/logs/access.log (deflated 94%)
  adding: site/index.html (deflated 91%)
  adding: site/about.html (deflated 87%)
  adding: site/.htaccess (deflated 9%)

壓縮好的 zip 檔裡有什麼,不必解壓縮,用 unzip -l 就能確認。放進去的檔案大小合計為 35,825 位元組,用 ls -l 看到的 site.zip 則是 4,158 位元組。

unzip -l site.zip
Archive:  site.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  10-28-2026 22:10   site/
        0  10-28-2026 22:10   site/css/
     4431  10-28-2026 22:10   site/css/style.css
        0  10-28-2026 22:10   site/js/
     1882  10-28-2026 22:10   site/js/app.js
        0  10-28-2026 22:10   site/logs/
    24134  10-28-2026 22:10   site/logs/access.log
     3723  10-28-2026 22:10   site/index.html
     1612  10-28-2026 22:10   site/about.html
       43  10-28-2026 22:10   site/.htaccess
---------                     -------
    35825                     10 files
ls -l site.zip
-rw-r--r-- 1 root root 4158 Oct 28 23:05 site.zip

2️⃣ zip 指令選項

  • ↪️ -x、-u、-d、壓縮等級、-e

zip 指令選項中,-x 會用樣式排除不要放進去的檔案。為了排除記錄資料夾而指定 [site/logs/*] 時,logs 資料夾和 access.log 會被排除,只放進八個項目。

zip -r site-nolog.zip site -x 'site/logs/*'
  adding: site/ (stored 0%)
  adding: site/css/ (stored 0%)
  adding: site/css/style.css (deflated 93%)
  adding: site/js/ (stored 0%)
  adding: site/js/app.js (deflated 87%)
  adding: site/index.html (deflated 91%)
  adding: site/about.html (deflated 87%)
  adding: site/.htaccess (deflated 9%)

為了避免 shell 先把 [*] 換成檔案名稱,樣式要用單引號括起來。官方手冊為了同樣目的,舉了在 [*] 前面加反斜線的例子。

-u 只會把建立 zip 檔之後有變更的檔案重新放進去。在 index.html 結尾加一行後用 -ru 執行,只有那一個檔案以 [updating] 重新放進去。

zip -ru site.zip site
updating: site/index.html (deflated 90%)

在沒有檔案變更的狀態下再執行一次同樣的指令,什麼都沒有輸出,結束代碼是 12。官方手冊中 12 表示 zip 沒有事情可做。

zip -ru site.zip site; echo $?
12

要刪除 zip 檔裡的項目時使用 -d。指定 [site/logs/*] 後,logs 資料夾項目和 access.log 會從 site.zip 刪除。

zip -d site.zip 'site/logs/*'
deleting: site/logs/
deleting: site/logs/access.log

-0 到 -9 的數字是壓縮等級,依官方手冊預設值是 -6。把同一個 site 資料夾用 -q 不輸出地壓縮三次來比較大小。因為在 -u 時已在 index.html 加了一行,此時原始檔案合計為 35,875 位元組。

zip -qr -0 level0.zip site
zip -qr level6.zip site
zip -qr -9 level9.zip site
ls -l level*.zip
-rw-r--r-- 1 root root 37433 Oct 28 23:05 level0.zip
-rw-r--r-- 1 root root  4179 Oct 28 23:05 level6.zip
-rw-r--r-- 1 root root  3874 Oct 28 23:05 level9.zip
壓縮等級zip 檔大小與原始合計相比
原始檔案合計35,875 位元組100%
-0 (不壓縮)37,433 位元組104.3%
-6 (預設值)4,179 位元組11.6%
-9 (最大壓縮)3,874 位元組10.8%

用 -0 建立的 37,433 位元組比原始合計 35,875 位元組還大。這是未壓縮的資料,再加上每個項目附帶的檔案名稱、時間等標頭資訊 1,558 位元組的大小。-9 比預設值小 305 位元組,差異全部來自最大的檔案 access.log。

要設定密碼時加上 -e。密碼要輸入兩次,輸入的字元不會顯示在畫面上。

zip -e secret.zip memo.txt
Enter password:
Verify password:
  adding: memo.txt (stored 0%)

用 -e 建立的 secret.zip,用 unzip -l 查看時,檔案名稱、大小和日期也不需要密碼就會顯示。只有檔案內容會加密,所以不適合連檔案名稱都要隱藏的資料。

unzip -l secret.zip
Archive:  secret.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       71  10-28-2026 22:10   memo.txt
---------                     -------
       71                     1 file

把密碼直接寫在命令列的 -P,是官方手冊警告不安全的選項,因為同一台伺服器的其他使用者可以看到正在執行的指令。手冊也說明 zip 的標準加密本身相對較弱。

解壓縮用 -e 建立的 zip 時,unzip 會逐一詢問每個檔案的密碼,輸入正確後該檔案就會解壓縮。

unzip secret.zip -d dec
Archive:  secret.zip
[secret.zip] memo.txt password:
 extracting: dec/memo.txt

📌 主要選項:本文實際執行的結果

選項作用本文看到的結果
zip -r把資料夾內的子資料夾和檔案全部放進去不加 -r 只有 adding: site/ 一行,加上 -r 後連 .htaccess 共十個項目
zip -x用樣式排除不要放進去的檔案排除 site/logs/* 後,沒有 logs 資料夾和 access.log,只放進八個項目
zip -u只把建立 zip 檔後有變更的檔案重新放進去updating: site/index.html (deflated 90%) 一行,沒有變更時不輸出、結束代碼 12
zip -d刪除 zip 檔裡的項目deleting: site/logs/ 和 deleting: site/logs/access.log
zip -q隱藏壓縮時輸出的行用 -qr 壓縮的三個指令(-0、預設、-9)都沒有 adding 行就結束
zip -0不壓縮只存入level0.zip 為 37433 位元組,比原始合計還大
zip -9壓縮到最小(預設 -6)level9.zip 3874 位元組,用預設值建立的 level6.zip 4179 位元組
zip -e詢問密碼並加密內容詢問 Enter password: 和 Verify password:,然後 adding: memo.txt (stored 0%)
unzip -l不解壓縮,輸出存入的項目清單site.zip 有 10 files,合計 35825 位元組
unzip -d指定解壓縮的資料夾,不存在的資料夾會新建以 creating: restore/site/ 開始,site 資料夾結構解壓縮到 restore 底下
unzip -o不詢問就覆寫所有已存在的檔案沒有詢問,五行 inflating
unzip -n不詢問就略過已存在的檔案Archive: site.zip 那一行之後什麼都沒有
unzip -t不解壓縮,檢查壓縮資料每個項目 OK 和 No errors detected,被截斷的 broken.zip 則是 End-of-central-directory signature not found
unzip -O指定 zip 內檔案名稱的字元編碼用 -O CP949 後,亂碼名稱顯示為 보고서.txt、견적서.txt(韓文的「報告書」「估價單」)
unzip -p不建立檔案,把解壓縮的內容送到標準輸出不建立檔案,只輸出 .htaccess 的兩行

📎 其他選項:依官方手冊

選項作用
zip -m壓縮後刪除原始檔案和資料夾(在 zip 檔完全建立後才刪除,官方手冊建議與 -T 一起使用)
zip -j只存入檔案名稱,不含資料夾路徑
zip -y把符號連結以連結本身存入,而不是它指向的檔案
zip -T檢查建立的 zip 檔,檢查失敗時不變更原本的 zip 檔
zip -f只用變更後的檔案取代 zip 檔中已有的項目,不加入新檔案
zip -i只放進符合指定樣式的檔案
zip -P把密碼直接寫在命令列(官方手冊警告不安全)
unzip -j不建立 zip 內的資料夾結構,只把檔案解壓縮到一個資料夾
unzip -x指定不解壓縮的項目
unzip -q隱藏解壓縮時輸出的行
unzip -u只解壓縮磁碟上沒有的檔案和較新的檔案
unzip -v清單中也輸出壓縮方式、壓縮後大小、比例和 CRC

3️⃣ unzip 指令用法

  • ↪️ 在 Linux 解壓縮 zip 與 -d、-o、-n

在 Linux 解壓縮 zip 檔時,在 unzip 後面寫 zip 檔名稱。沒有用 -d 指定解壓縮資料夾時,會解壓縮到目前的資料夾(依官方手冊),所以這次指定了 restore 資料夾。

unzip site.zip -d restore
Archive:  site.zip
   creating: restore/site/
   creating: restore/site/css/
  inflating: restore/site/css/style.css
   creating: restore/site/js/
  inflating: restore/site/js/app.js
  inflating: restore/site/index.html
  inflating: restore/site/about.html
  inflating: restore/site/.htaccess

用 -d 指定的資料夾不存在時,unzip 會新建,並在底下照原樣解壓縮出 site 資料夾結構。

在同一個位置再解壓縮一次,unzip 每遇到已存在的檔案就會停下來詢問是否覆寫。選擇 [N]one 會在不覆寫任何剩餘檔案的情況下結束。

unzip site.zip -d restore
Archive:  site.zip
replace restore/site/css/style.css? [y]es, [n]o, [A]ll, [N]one, [r]ename: N

不想被詢問時,加上 -o 或 -n。-o 會覆寫所有已存在的檔案,-n 則略過已存在的檔案。官方手冊把 -o 列為危險選項,所以加上 -o 前要先確認那個資料夾可以覆寫。

unzip -o site.zip -d restore
Archive:  site.zip
  inflating: restore/site/css/style.css
  inflating: restore/site/js/app.js
  inflating: restore/site/index.html
  inflating: restore/site/about.html
  inflating: restore/site/.htaccess
unzip -n site.zip -d restore
Archive:  site.zip

在檔案都已存在的位置用 -n 解壓縮時,Archive 那一行之後什麼都不會出現。

只解壓縮 zip 檔的一部分時,在 zip 檔名稱後面寫路徑或樣式。指定 [site/css/*] 後,只會解壓縮 css 資料夾和 style.css。

unzip site.zip 'site/css/*' -d part
Archive:  site.zip
   creating: part/site/css/
  inflating: part/site/css/style.css

4️⃣ unzip 錯誤

  • ↪️ End-of-central-directory 與檔案名稱亂碼

從其他伺服器收到的 zip 檔,解壓縮前先用 unzip -t 檢查。壓縮資料沒有問題時,每個項目會顯示 OK,最後一行顯示 [No errors detected]。

unzip -t site.zip
Archive:  site.zip
    testing: site/                    OK
    testing: site/css/                OK
    testing: site/css/style.css       OK
    testing: site/js/                 OK
    testing: site/js/app.js           OK
    testing: site/index.html          OK
    testing: site/about.html          OK
    testing: site/.htaccess           OK
No errors detected in compressed data of site.zip.

傳輸中斷而後半部被截斷的 zip,以 [End-of-central-directory signature not found] 結束。這是對 broken.zip 用 -t 檢查的結果;broken.zip 只保留了 site.zip 前 2,000 位元組,而 site.zip 在用 -d 刪除 logs 項目後為 2,436 位元組。

unzip -t broken.zip
Archive:  broken.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of broken.zip or
        broken.zip.zip, and cannot find broken.zip.ZIP, period.

unzip 會先讀取 zip 檔最後面的檔案清單(central directory)。所以後半部被截斷的 broken.zip 連第一個項目都無法檢查。

放入不是 zip 的檔案也出現同樣的訊息。用 unzip 開啟以 tar 打包的 site.tar.gz,也以同樣的錯誤結束,tar.gz 檔則用 tar 指令解壓縮。

unzip site.tar.gz
Archive:  site.tar.gz
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of site.tar.gz or
        site.tar.gz.zip, and cannot find site.tar.gz.ZIP, period.

在 Windows 建立的 zip 檔,有時檔案名稱不是 UTF-8,而是以 Windows 的字元編碼存入。在 UTF-8 伺服器上用 unzip -l 開啟名稱以 CP949 存入的 report-win.zip,檔案名稱變成無法辨識的符號。

unzip -l report-win.zip
Archive:  report-win.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       26  10-28-2026 22:10   ║╕░э╝н.txt
       17  10-28-2026 22:10   ░▀└√╝н.txt
---------                     -------
       43                     2 files

在 -O 後面寫上建立該 zip 的 Windows 字元編碼,名稱就會正確顯示,用同樣選項解壓縮的檔案也會以原本的名稱建立。

unzip -O CP949 -l report-win.zip
Archive:  report-win.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       26  10-28-2026 22:10   보고서.txt
       17  10-28-2026 22:10   견적서.txt
---------                     -------
       43                     2 files
unzip -O CP949 report-win.zip -d win
Archive:  report-win.zip
  inflating: win/보고서.txt
  inflating: win/견적서.txt

依 unzip 說明,-O 用於在 DOS·Windows 建立的 zip,-I 用於在其他系統建立的 zip。

解壓縮時出現的錯誤訊息與接下來要確認的事項如下。

輸出說明接下來要確認的項目
End-of-central-directory signature not found找不到 zip 檔結尾的檔案清單 — 結尾被截斷的檔案或不是 zip 的檔案與原檔比較大小後重新下載 · tar.gz 用 tar
cannot find or open目前資料夾中沒有該名稱的檔案用 ls 確認檔案名稱和位置
skipping: … incorrect password密碼錯三次而略過該檔案(結束代碼 82)密碼的大小寫和輸入法語言
檔案名稱變成符號亂碼名稱以 Windows 字元編碼儲存在 -O 後面寫該編碼(CP949 等)

🔲 zip 指令模擬器

在下方的模擬器中,可以親手輸入用 zip 壓縮,再用 unzip 查看清單或解壓縮的過程。

zip 指令模擬器Zip 3.0 · UnZip 6.00
[root@server work]#
只在瀏覽器中運作,不會壓縮實際檔案。壓縮率是實測值,在模擬器中新建立的檔案則是估計值。用 ↑↓ 可以叫出之前輸入的指令。

🔢 FAQ & 推薦內容

用 unzip 開啟以 gzip 壓縮的 .gz 檔,以 [End-of-central-directory signature not found] 結束。用 [gzip -d readme.txt.gz] 解壓縮後,readme.txt.gz 變回 readme.txt。先用 tar 打包多個檔案、再用 gzip 壓縮的 .tar.gz,用 tar 一次解開。

在確認過的 AlmaLinux 9 伺服器上,zip 和 unzip 分別是 zip、unzip 套件安裝的指令。所以 RHEL 系列用 [sudo dnf install zip unzip],Debian 系列用 [sudo apt install zip unzip] 安裝(依 Debian 官方套件清單)。

在 unzip -p 後面寫 zip 檔和裡面的路徑,內容就會直接顯示在畫面上。執行 [unzip -p site.zip site/.htaccess] 後,沒有建立檔案,只輸出 .htaccess 的兩行。

ℹ️ 聯盟行銷說明
本站內容包含聯盟行銷連結。訪客透過這些連結購買商品或服務時,本站會從賣家獲得佣金。在這個過程中,購買者支付的金額(遇到活動折扣時金額會降低↓)並不會因此提高。刊登的價格、折扣與庫存資訊以撰寫當時為準,可能與實際情況不同,購買前請向賣家做最後確認。商品的挑選與評價依本站自訂的標準撰寫,是否支付佣金不會影響介紹順序或評價內容。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Prove your humanity: 1   +   3   =