Search code examples
powershellazure-devopscompressionazure-pipelinescompress-archive

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.


Solution

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