Let me preface by stating that I’ somewhat new to dealing with zipping/unzipping/reading/reading files. That being said, I’m doing a PoC that will retrieve data via api and write the responses to a database. The response is a zip file and inside this zip is the json data I will be reading and writing to the database.
I’m having some trouble unzipping and reading the information. Please find the code below:
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(baseUrl),
Headers =
{
{ "X-API-TOKEN", apiKey },
},
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
// here is where I am stuck - not sure how I would unzip and read the contents
}
Thanks
2
Answers
You can convert
body
to abyte
array and then unzip it usingMemoryStream
.Assuming you actually have a
.zip
file, you don’t need aMemoryStream
, you just need to pass the existing stream toZipArchive