如何使用 PowerShell 壓縮/解壓縮檔案或資料夾?


現在,使用 PowerShell 壓縮或提取(解壓縮)檔案或資料夾變得很容易。PowerShell 從 5.1 版本開始添加了存檔模組 **(Microsoft.PowerShell.Archive)** 的功能。

如果您使用的是較舊版本的 PowerShell(版本 4.0 或更低版本),則可以從網站或透過命令列下載並安裝該模組。但是,僅下載和安裝或手動將 Archive 模組複製到 Module 資料夾不起作用,因為它需要一些由 **.Net framework 4.5** 提供的依賴 DLL 檔案,因此如果您使用的 **.Net framework** 版本低於 **4.5**,則也需要考慮升級。

一旦您擁有了合適的 PowerShell 版本和 .Net framework 版本,並且如果您已安裝 Archive 模組或它是預設安裝的,請檢查如下所示的命令是否受存檔模組支援。

PS C:\WINDOWS\system32> Get-Command -Module *Archive* |ft -AutoSize
CommandType Name                            Version Source
----------- ----                            ------- ------
Function Compress-Archive              1.0.1.0 Microsoft.PowerShell.Archive
Function Expand-Archive                1.0.1.0 Microsoft.PowerShell.Archive

因此,此模組包含兩個命令。**Compress-Archive** 命令用於壓縮檔案和資料夾,而 **Expand-Archive** 命令用於提取壓縮資料。

Compress-Archive 命令用於壓縮檔案和資料夾。

在開始 Compress-Archive 命令的示例之前,我們首先了解一下 cmdlet 語法。

Compress-Archive
   -LiteralPath <String[]>
   [-DestinationPath] <String>
   [-CompressionLevel <String>]
   -Force
   -Update
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

這裡,主要引數是 **原始檔/資料夾、目標路徑、壓縮級別、強制** 和 **更新**。

壓縮級別

壓縮級別有三種類型。

  • **Fastest** - 使用最快的壓縮來減少處理時間。這可能導致檔案大小更大。
  • **Optimal** - 正常的壓縮級別。處理時間取決於檔案的大小。如果未使用壓縮引數,則這是預設的壓縮級別。
  • **NoCompression** - 不壓縮原始檔。這意味著壓縮後文件夾和檔案的大小與原始大小相同,只是沒有壓縮比率。

強制

指定此引數時,它將覆蓋檔案和資料夾。

更新

此引數將更新從源到目標的較新或已更新的檔案,而不是再次壓縮整個內容。

示例

1. 壓縮單個資料夾。

Compress-Archive C:\temp\* -DestinationPath c:\temp.zip -CompressionLevel
Fastest

以上命令將壓縮資料夾 **C:\temp** 到目標路徑 C:\,ZIP 檔名為 **temp.zip**,並且壓縮級別為 Fastest。

如果您嘗試使用已存在的相同目標名稱壓縮資料夾,則會彈出錯誤。

PS E:\scripts\Powershell> Compress-Archive C:\temp\* -DestinationPath
c:\temp.zip -CompressionLevel Fastest
Compress-Archive : The archive file C:\temp.zip already exists. Use the -
Update parameter to update the existing
archive file or use the -Force parameter to overwrite the existing archive
file.
At line:1 char:1
+ Compress-Archive C:\temp\* -DestinationPath c:\temp.zip -CompressionL ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\temp.zip:String) [Compress-
Archive], IOException
+ FullyQualifiedErrorId : ArchiveFileExists,Compress-Archive

要覆蓋目標檔案,您需要使用 –Force 引數。

Compress-Archive C:\temp\* -DestinationPath c:\temp.zip -CompressionLevel
Fastest -Force

如果您想更新 temp 資料夾中的任何檔案,並且需要更新目標 zip 檔案,則需要使用 **–Update** 引數。當您使用 **Update** 引數時,不需要使用 **-Force** 引數,因為 **–Update** 引數也會覆蓋目標檔案並更新檔案,即使目標檔案不存在,它也會建立一個新檔案。

Compress-Archive C:\temp\* -DestinationPath c:\temp.zip -CompressionLevel
Fastest -Update

2. 壓縮多個檔案和資料夾。

不同位置的多個檔案 -

$compress = @{
   Path = "C:\temp\ComputerInformation.csv","D:\cars.xml"
   DestinationPath = "C:\archive.zip"
   CompressionLevel = "Fastest"
}
Compress-Archive @compress -Verbose

不同位置的多個資料夾 -

$compress = @{
   Path = "C:\temp\*","C:\Dell\*"
   DestinationPath = "C:\folderarchive.zip"
   CompressionLevel = "Fastest"
}
Compress-Archive @compress -Force -Verbose

Expand-Archive 命令。

**Expand-Archive** 命令用於提取 ZIP 檔案。與 **Compress-Archive** 命令相比,此命令相對簡單,因為它只需要原始檔和目標 ZIP 檔案。

在下面的示例中,我們將提取 folderarchive.zip 檔案到目標位置。

Expand-Archive C:\folderarchive.zip -DestinationPath C:\Unzipedfolder -Verbose

這裡,目標資料夾 **UnzipedFolder** 在目標路徑中不存在,但如果不存在,cmdlet 會自動建立該資料夾。如果資料夾已存在,則需要使用 –Force 引數來覆蓋檔案和資料夾。

Expand-Archive C:\folderarchive.zip -DestinationPath C:\Unzipedfolder –Force
-Verbose

更新於: 2020-07-13

2K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告