Muhammedh
Muhammedh

Reputation: 504

Powershell - Compress files based on the contents of another zip file

I have a zip file that has to be extracted to a destination folder. Before I extract I want to backup ONLY the root files and the sub directories that will be replaces by extracting the zip file.

Can I write up a script that will be find out the sub directories within the .zip and backup those from the destination folder (if they are available)?

And I will be using this script in Azure DevOps.

Upvotes: 0

Views: 471

Answers (2)

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41725

You can use PowerShell to discover the zip file content, check if it exists in the destination folder, if yes - do a backup. for example:

$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
    }
}

Upvotes: 1

Krzysztof Madej
Krzysztof Madej

Reputation: 40819

The easiest way it will be to extract file from your zip to some temporary folder (on Azure DevOps it could be folder behind this variable Agent.TempDirectory). Then you can copy files you want to backup to another location like $(Agent.TempDirectory)/backup and pack this folder and put in Build.ArtifactStagingDirectory if you want to publish them. After this you can extract zip again to your destinations as you already have your backup.

You may find this documentation useful if you want to more about above mentioned folders.

Upvotes: 0

Related Questions