如何使用PowerShell压缩/解压缩文件或文件夹?

阅读 2305 收藏 0 点赞 0 评论 0

现在,使用PowerShell可以轻松压缩或解压缩(解压缩)文件或文件夹。PowerShell已从PowerShell 5.1版本添加了存档模块(Microsoft.PowerShell.Archive)的功能。

如果您使用的是旧版PowerShell(4.0或更低版本),则可以从网站或通过命令行下载并安装模块。但是,仅下载和安装或手动将“存档”模块复制到“模块”文件夹是行不通的,因为它需要由.Net Framework 4.5提供的一些相关DLL文件,因此,如果它位于下面,则还需要考虑升级.Net Framework。4.5版本。

拥有适当的PowerShell版本和.Net框架版本后,如果已安装存档模块或默认安装,则检查存档模块支持的命令,如下所示。

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>]

在这里,主要参数是源文件/文件夹,目标路径,压缩级别,强制更新

压缩等级

有三种类型的压缩级别。

  • 最快-使用最快的压缩来减少处理时间。这可能会导致更大的文件大小。

  • 最佳-正常压缩级别。处理时间取决于文件的大小。如果不使用compression参数,则这是默认的压缩级别。

  • NoCompression-不压缩源文件。这意味着压缩后实际文件夹和文件的大小相同,只是没有压缩率。

指定此参数后,它将覆盖文件和文件夹。

更新资料

此参数将文件从源更新到目标,这些文件是较新的或更新的文件,而不是再次压缩整个内容。

例子

1.压缩单个文件夹。

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

上面的命令将使用ZIP名称temp.zip和压缩级别Fastest压缩目标路径C:\文件夹C:\ temp

如果您尝试使用已经存在的相同目的地名称压缩文件夹,则会弹出错误。

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文件很有用。该命令是一个简单的相对压缩存档命令,因为它仅需要源和目标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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号