我正在尝试使用以下代码从HttpWebResponse
对象中解压缩并读取文件:
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());
}
}
}
}
但获得System.IO.InvalidDataException: End of Central Directory record could not be found
。
我在做什么错了?
答案 0 :(得分:1)
这是我在使用GZipStream时的想法(仅作为示例,此处不能保证)...
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());
}