0

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.

Muhammedh
  • 484
  • 1
  • 11
  • 25

2 Answers2

1

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
    }
}
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • This is perfect :) Thanks... I tried to use copy-item to backup these files into a separate folder, and I was unable to preserve the directory structure. Copy-Item -Path $FullFilePath -Destination $(ReleaseClientWorkFolder)\$(Build.DefinitionName)\$(Build.BuildNumber)\PreReleaseCopies\CISIWeb2\$zipContent – Muhammedh Sep 24 '20 at 09:54
  • What is `$FullFilePath`? and I don't understand completely the issue... – Shayki Abramczyk Sep 24 '20 at 09:59
  • $FullFilePath is C:\inetpub\wwwroot\MyWeb2\bin\AjaxControlToolkit.dll For an example so when I copy this to lets says D:\Backup\MyWeb2, the copied file is always in the root, I want the file to maintain the same directory structure. So I want the file to be copied to D:\Backup\MyWeb2\bin\AjaxControlToolkit.dll. – Muhammedh Sep 24 '20 at 10:13
  • try to append the structure to the backup folder path or copy only the folders (with files) and not individual files – Shayki Abramczyk Sep 24 '20 at 10:16
  • The folder (Inetpub) has a lot more content (both folders and files) that I dont really need. That's my issue here. – Muhammedh Sep 24 '20 at 10:31
  • 1
    Solved it by doing a new-item before copy-item (https://stackoverflow.com/questions/48027762/powershell-copy-item-destination) Thanks for all the help :) – Muhammedh Sep 24 '20 at 11:34
0

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.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • I can do that, However the destination folder has heaps of directories and contents. I don't want to backup all the contents. – Muhammedh Sep 23 '20 at 10:31
  • @Muhammedh Yes, this is why I recommend you to extract it to clean folder like `$(Agent.TempDirectory)/MyCleanFolder` and get files only those you need to backup. And at the end extract to you destination folder. But clean folder to create backup is not your destination folder. – Krzysztof Madej Sep 23 '20 at 10:44
  • @krzysztof-medej How will I know which files to backup? I want to get the list of folders that I will be replacing. – Muhammedh Sep 23 '20 at 10:48
  • Ok. I see, so you don't know what is in destination folder. For that part please check this https://stackoverflow.com/questions/6526441/comparing-folders-and-content-with-powershell – Krzysztof Madej Sep 23 '20 at 10:53