azure-devopstfsazure-pipelinesazure-devops-rest-apiazure-sdk-.net

Download a Zip File from Azure DevOps Git Repo Using Azure DevOps SDK


I am trying to download a zip file from an Azure DevOps Git repository using the Azure DevOps SDK for .NET (dont want to use the plain REST APIs).

 public async Task DownloadItemAsync(string gitRepositoryName, string remotefilePath, string localFilePath)
 {
     VssConnection vssConnection = GetVssConnection();

     GitHttpClient gitHttpClient = vssConnection.GetClient<GitHttpClient>();

     var response = gitHttpClient.GetItemAsync(Settings.TfsProjectName, gitRepositoryName, remotefilePath, null, VersionControlRecursionType.None, null, null, download:true, null, true).Result;
}

How can I use the response object to download and save the zip file to a local path? Is there any better class/method from the SDK ?

Any guidance or examples would be greatly appreciated!


Solution

  • you may try GetItemZipAsync method

    var fileStream = File.Create("C:\\Temp\\my.zip");
    
    var zipstream = GitClient.GetItemZipAsync(TeamProjectName, GitRepoName, path: "/Readme.md").Result;
    
    zipstream.CopyTo(fileStream);
    
    fileStream.Close();