管理人Kのひとりごと

デジモノレビューやプログラミングや写真など

zipコマンド、unzipコマンド備忘

zipコマンドを使うとき、第一引数がzipファイル名?固めたいファイル名?となって毎回ググるので備忘。ついでに、unzipコマンドの使い方も備忘。

バージョン情報

[root@3192e8afd36a /]# zip
Zip 3.0 (July 5th 2008). 

[root@3192e8afd36a /]# unzip -V
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.

コマンド

検証時の構成

[root@3192e8afd36a work]# find ./ -type f
./dirA/dirC/fileC.txt
./dirA/fileA.txt
./dirB/fileB.txt

フォルダ構成を保って圧縮(zip -r)

[root@3192e8afd36a work]# zip -r all.zip .
  adding: dirA/ (stored 0%)
  adding: dirA/dirC/ (stored 0%)
  adding: dirA/dirC/fileC.txt (stored 0%)
  adding: dirA/fileA.txt (stored 0%)
  adding: dirB/ (stored 0%)
  adding: dirB/fileB.txt (stored 0%)

fileC.txtをfileC.zipとして圧縮(相対パスを指定する際にフォルダ構成を保たずに圧縮)(zip -j)

[root@3192e8afd36a work]# zip -j fileC.zip dirA/dirC/fileC.txt
  adding: fileC.txt (stored 0%)
[root@3192e8afd36a work]# unzip -l fileC.zip
Archive:  fileC.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  08-12-2021 14:49   fileC.txt
---------                     -------
        0                     1 file

既存のzipファイルにファイルを追加(zip)

[root@3192e8afd36a work]# zip -j fileC.zip dirA/fileA.txt
  adding: fileA.txt (stored 0%)
[root@3192e8afd36a work]# unzip -l fileC.zip
Archive:  fileC.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  08-12-2021 14:49   fileC.txt
        0  08-12-2021 14:49   fileA.txt
---------                     -------
        0                     2 files

zipファイルの中身を解凍せずに確認(unzip -l)

[root@3192e8afd36a work]# unzip -l all.zip
Archive:  all.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  08-12-2021 14:49   dirA/
        0  08-12-2021 14:49   dirA/dirC/
        0  08-12-2021 14:49   dirA/dirC/fileC.txt
        0  08-12-2021 14:49   dirA/fileA.txt
        0  08-12-2021 14:49   dirB/
        0  08-12-2021 14:49   dirB/fileB.txt
---------                     -------
        0                     6 files

ファイルの解凍(unzip)

[root@3192e8afd36a wk]# unzip all.zip
Archive:  all.zip
   creating: dirA/
   creating: dirA/dirC/
 extracting: dirA/dirC/fileC.txt
 extracting: dirA/fileA.txt
   creating: dirB/
 extracting: dirB/fileB.txt

[root@3192e8afd36a wk]# find ./
./
./all.zip
./fileC.zip
./dirA
./dirA/dirC
./dirA/dirC/fileC.txt
./dirA/fileA.txt
./dirB
./dirB/fileB.txt