0

I've been trying to download a ZIP file from azure API which contains folder in it. If I download ZIP using browser, it is fine. The problem is if I try to download with code

Code itself downloaded zip, but when I try to open with Windows Explorer: WindowsZip

When opening it through 7-zip it shows this: 7-zip

For comparison, proper zip downloaded with browser: proper zip

public async Task GetArtifact(string url)
        {
            Stream content;
            var client = CreateClient();
            var uri = new Uri(url);
            var percentage = 0.0;

            HttpResponseMessage response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);
            var fileSize = response.Content.Headers.ContentLength;
            var fileName = response.Content.Headers.ContentDisposition.FileName;
            response.Content.Headers.ContentType.MediaType = "application/zip";
            if (response.IsSuccessStatusCode)
            {
                content = await response.Content.ReadAsStreamAsync();
                var file = File.OpenWrite(fileName);
                var buffer = new byte[32768];
                var readByte = 0L;
                var isAllRead = false;
                do
                {
                    var reader = await content.ReadAsync(buffer, 0, buffer.Length);
                    if (reader == 0)
                    {
                        isAllRead = true;
                    }
                    else
                    {
                        await file.WriteAsync(buffer, 0, reader);
                        readByte += reader;
                        if(fileSize.HasValue)
                        {
                            percentage = Math.Round((double)readByte / fileSize.Value * 100, 2);
                            Console.WriteLine(percentage);
                        }
                    }
                } while (!isAllRead);
                file.Dispose();
            }
            else
            {
                throw new WebException(response.ToString());
            }
        }
  • The response is gzipped, probably. Provide a [mre]. – CodeCaster Mar 07 '23 at 17:00
  • Maybe you got an error message rather than a zip file? Try opening the file in a text editor / hex editor. It's also worth seeing whether it starts with the [zip magic bytes](https://en.wikipedia.org/wiki/List_of_file_signatures) – canton7 Mar 07 '23 at 17:01
  • 1
    Also, be aware that `File.OpenWrite` [is a foot-gun](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.openwrite): "*For an existing file, it does not append the new text to the existing text. Instead, it overwrites the existing characters with the new characters. **If you overwrite a longer string (such as "This is a test of the OpenWrite method") with a shorter string (such as "Second run"), the file will contain a mix of the strings ("Second runtest of the OpenWrite method").***". You probably want `File.Create` – canton7 Mar 07 '23 at 17:04
  • @CodeCaster indeed it was gzipped, After adding HttpClientHandler with GZip & deflate decompression method to HttpClient it worked – Damian Pietras Mar 07 '23 at 17:33

0 Answers0