はじめに
コジマです。
zipやらrarやらtarなんかを解凍するのに便利なフリーソフトの7-zip。
右クリックのコンテキストメニューから使用する人がほとんどなのではないでしょうか。
実はコマンドラインで使うことができるんですね。
その方法を紹介したいと思います。
環境変数を設定する
まず、環境変数PATHに7-zipのインストール先を設定してどこでも7zipのコマンドを使えるようにします。
「コントロールパネル」=>「システムとセキュリティ」=>「システム」=>「システムの詳細設定」をクリックします。
システムのプロパティが開くので、「詳細設定」タブの「環境変数」をクリックします。
「システム環境変数」の「Path」を編集して、7-zipのインストール先のパスを設定します。
自分はデフォルトの「C:\Program Files\7-Zip」としています。
コマンドの実行確認
PowerShellを開いて、「7z」コマンドで以下のようにヘルプが出てくればOKです!
> 7z 7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21 Usage: 7z[ ...] [ ...] [@listfile] a : Add files to archive b : Benchmark d : Delete files from archive e : Extract files from archive (without using directory names) h : Calculate hash values for files i : Show information about supported formats l : List contents of archive rn : Rename files in archive t : Test integrity of archive u : Update files to archive x : eXtract files with full paths -- : Stop switches and @listfile parsing -ai[r[-|0]]{@listfile|!wildcard} : Include archives -ax[r[-|0]]{@listfile|!wildcard} : eXclude archives -ao{a|s|t|u} : set Overwrite mode -an : disable archive_name field -bb[0-3] : set output log level -bd : disable progress indicator -bs{o|e|p}{0|1|2} : set output stream for output/error/progress line -bt : show execution time statistics -i[r[-|0]]{@listfile|!wildcard} : Include filenames -m{Parameters} : set compression Method -mmt[N] : set number of CPU threads -mx[N] : set compression level: -mx1 (fastest) ... -mx9 (ultra) -o{Directory} : set Output directory -p{Password} : set Password -r[-|0] : Recurse subdirectories -sa{a|e|s} : set Archive name mode -scc{UTF-8|WIN|DOS} : set charset for for console input/output -scs{UTF-8|UTF-16LE|UTF-16BE|WIN|DOS|{id}} : set charset for list files -scrc[CRC32|CRC64|SHA1|SHA256|*] : set hash function for x, e, h commands -sdel : delete files after compression -seml[.] : send archive by email -sfx[{name}] : Create SFX archive -si[{name}] : read data from stdin -slp : set Large Pages mode -slt : show technical information for l (List) command -snh : store hard links as links -snl : store symbolic links as links -sni : store NT security information -sns[-] : store NTFS alternate streams -so : write data to stdout -spd : disable wildcard matching for file names -spe : eliminate duplication of root folder for extract command -spf : use fully qualified file paths -ssc[-] : set sensitive case mode -sse : stop archive creating, if it can't open some input file -ssw : compress shared files -stl : set archive timestamp from the most recently modified file -stm{HexMask} : set CPU thread affinity mask (hexadecimal number) -stx{Type} : exclude archive type -t{Type} : Set type of archive -u[-][p#][q#][r#][x#][y#][z#][!newArchiveName] : Update options -v{Size}[b|k|m|g] : Create volumes -w[{path}] : assign Work directory. Empty path means a temporary directory -x[r[-|0]]{@listfile|!wildcard} : eXclude filenames -y : assume Yes on all queries
サンプル
zipを作る
test1.txtとtest2.txtをtest.zipにアーカイブする
7z a test.zip .\test1.txt .\test2.txt
7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21 Scanning the drive: 2 files, 0 bytes Creating archive: test.zip Add new data to archive: 2 files, 0 bytes Files read from disk: 2 Archive size: 282 bytes (1 KiB) Everything is Ok
zipの中身を確認する
test.zipの中を確認する
7z l test.zip
7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21 Scanning the drive for archives: 1 file, 282 bytes (1 KiB) Listing archive: test.zip -- Path = test.zip Type = zip Physical Size = 282 Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ------------------------ 2021-06-07 00:07:30 ....A 0 0 test1.txt 2021-06-07 00:07:30 ....A 0 0 test2.txt ------------------- ----- ------------ ------------ ------------------------ 2021-06-07 00:07:30 0 0 2 files
zipの中のファイル名を変更する
test.zipの中のtest1.txtをtesttest1.txtという名前にする
7z rn test.zip test1.txt testtest1.txt
7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21 Open archive: test.zip -- Path = test.zip Type = zip Physical Size = 282 Updating archive: test.zip Keep old data in archive: 2 files, 0 bytes Add new data to archive: 0 files, 0 bytes Files read from disk: 0 Archive size: 290 bytes (1 KiB) Everything is Ok
zipを展開する
test.zipを展開する
7z e .\test.zip
7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21 Scanning the drive for archives: 1 file, 290 bytes (1 KiB) Extracting archive: .\test.zip -- Path = .\test.zip Type = zip Physical Size = 290 Everything is Ok Files: 2 Size: 0 Compressed: 290
さいごに
7-zipをコマンドラインで使う方法を簡単に紹介してみました。
PowerShellとかでスクリプト組んだりするときに便利です。
僕は大量のログファイルが入ったtarファイルを展開して1つのファイルにマージするスクリプトを書いたりしていました。
普段の業務を自動化したいけど、その中で圧縮ファイル扱わないといけないんだよなって時にめちゃ便利です。
もっと詳しく知りたい人は調べてみてくださいね~。
この記事を面白いまたは役に立ったと思ってくれた方は是非私のTwitter(@kojimanotech)を
フォローしてくれたらうれしいです!
以上、コジマでした。