首页 > 解决方案 > Powershell - 根据另一个 zip 文件的内容压缩文件

问题描述

我有一个必须解压缩到目标文件夹的 zip 文件。在我提取之前,我只想备份文件和将通过提取 zip 文件替换的子目录。

我可以编写一个脚本来找出 .zip 中的子目录并从目标文件夹中备份这些子目录(如果它们可用)?

我将在 Azure DevOps 中使用这个脚本。

标签: powershellazure-devopscompressionazure-pipelinescompress-archive

解决方案


您可以使用 PowerShell 发现 zip 文件内容,检查它是否存在于目标文件夹中,如果存在 - 进行备份。例如:

$ZipFilePath = "C:\Users\sabramczyk\Documents\Scrum.zip"
$DestinationFolder = "c:\test"

Add-Type -assembly "System.IO.Compression.FileSystem"
    
if (![System.IO.File]::Exists($ZipFilePath)) {
    throw "Zip file ""$ZipFilePath"" not found."
}

$ZipContent = ([System.IO.Compression.ZipFile]::OpenRead($ZipFilePath)).Entries.FullName
foreach($zipContent in $ZipContent)
{
    if(Test-Path $DestinationFolder+"/"+$zipContent)
    {
        # Do backup
    }
}

推荐阅读