adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.28k stars 480 forks source link

OpenEntryStream returns empty stream #618

Open nikoslyk203 opened 3 years ago

nikoslyk203 commented 3 years ago

I have downloaded a zip file as a stream through HttpClient in C# and I am trying to open one of the files inside the archive as a stream but for some reason when I use OpenEntryStream the stream returned is empty. Am I doing something wrong? The Zip File isn't corrupted because I tried to unzip the files to my disk and it did it successfully.

var zipRequest = httpClient.GetAsync(url).Result;
using (var zipStream = zipRequest.Content.ReadAsStreamAsync().Result)
using (var archive = SharpCompress.Archives.Zip.ZipArchive.Open(zipStream))
using (var xlsFileStream = archive.Entries.Where(x => x.Key == fileName).FirstOrDefault().OpenEntryStream())
{

}
adamhathcock commented 3 years ago

Archives expect a random access stream. I don't know what HttpClient is doing with the response stream of that but it could not be buffered.

You should use Reader or buffer the stream yourself (to memory or a file) and see what happens.

Also, use async/await instead of .Result