在 Batch Script 壓縮檔案
紀錄如何在 Batch Script 中壓縮檔案

因為目前公司沒有專職 SRE 或 DevOps 幫忙做 CI/CD,身為小小後端工程師只好自己想辦法,偏偏目前用到的 tech stack 包括 .net framework,所以必須跟 Windows 環境的 batch file 打交道來替代習慣的 shell 檔,其中有個步驟必須要壓縮/解壓縮資料夾

Tar

研究了一下,很興奮的發現 tar 在 command prompt 跟 powershell 都可以用,那代表我在 batch 檔裡面使用沒問題RRR

所以可以這樣用

:: zip
tar -cvzf "path and name to archive.tar" "path to folder to compress"

:: unzip
tar -xvzf "path and name to archive.tar"

Microsoft.Powershell.Archive Module

很可惜,這個方法有一個小小的問題

tar 基本上是從 Windows 10 或 Windows Server 2019 才開始支援,很不巧,我手上有個環境是 Windows Server 2016…

基於可以用原生就絕不多安裝東西的自虐原則,好險又找到了另一個方法,powershell 有個 archive 的模組可以用!

# zip
Compress-Archive -Path "path to folder to compress" -DestinationPath "path and name to archive.zip"

# unzip
Expand-Archive -Path "path and name to archive.zip" -DestinationPath "path to unzip archive.zip"

參考連結


Last modified on 2021-12-16