shmnff
shmnff

Reputation: 739

C# unzip and read file from HttpWebResponse

I'm trying to unzip and read file from HttpWebResponse object with followed code:

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (MemoryStream ms = new MemoryStream())
{
    response.GetResponseStream().CopyTo(ms);
    ms.Seek(0, SeekOrigin.Begin);

    using (ZipArchive za = new ZipArchive(ms, ZipArchiveMode.Read))
    {
        foreach (ZipArchiveEntry zae in za.Entries)
        {
            using (StreamReader sr = new StreamReader(zae.Open(), Encoding.GetEncoding(1251), true, 2 << 18))
            {
                Console.WriteLine(sr.ReadLine());
            }
        }
    }
}

but getting System.IO.InvalidDataException: End of Central Directory record could not be found. What i'm doing wrong?

Upvotes: 2

Views: 517

Answers (1)

MikeFromCanmore
MikeFromCanmore

Reputation: 162

Here is kind of what I was thinking with GZipStream (just an example, no guarantees here)...

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (GZipStream gzipStream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
using (StreamReader sr = new StreamReader(gzipStream))
{
    Console.Write(sr.ReadToEnd());
}

Upvotes: 2

Related Questions