如何提取从HttpWebResponse收到的压缩文件? [英] How to extract zipped file received from HttpWebResponse?

查看:273
本文介绍了如何提取从HttpWebResponse收到的压缩文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将url放在浏览器的地址栏中,它将zip文件下载到HD.压缩文件的大小为386字节,写在其属性中.

I put url to browser's address bar and it downloads the zip file to HD. The size of zipped file is 386 bytes as written in its properties.

当我使用UnZipFiles方法提取文件时-它可以工作. 但是,我想下载程序化并将其提取到内存中.我使用GetResultFromServer方法获取压缩内容.如标题所示,内容的大小与保存在HD上的压缩文件的大小相同:

When I use UnZipFiles method to extract the file - it works. But, I want to download programaticaly and extract it in memory. I use GetResultFromServer method to get zipped content. As shown in headers the size of the content is the same as the size of zipped file saved on HD:

content-disposition: attachment; filename=emaillog-xml.zip
Content-Length: 386
Cache-Control: private
Content-Type: application/zip
Date: Mon, 10 Sep 2012 08:28:28 GMT
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET

我的问题是如何提取GetResultFromServer返回的内容? 我尝试了以下方法:

My question is how to extract the content returned by GetResultFromServer? I tried the following:

var ms = new MemoryStream(Encoding.Unicode.GetBytes(res))
var s = new ZipInputStream(ms);

但是我得到了Unable to read from this stream.

已更新

我尝试了var zipStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), CompressionMode.Decompress),但出现了The magic number in GZip header is not correct错误

I tried var zipStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), CompressionMode.Decompress) but I get The magic number in GZip header is not correct error

代码

private string GetResultFromServer(ElasticLogParams elasticLogParams)
{        
    var webRequest = (HttpWebRequest)WebRequest.Create(url);            
    var response = webRequest.GetResponse();

    using (var reader = new StreamReader(response.GetResponseStream()))
    {
        var res = reader.ReadToEnd();
        var headers = response.Headers.ToString();              

        return res;
    }
}

public static void UnZipFiles(string zippedFilePath, Stream stream = null)
{
    var s = new ZipInputStream(stream ?? File.OpenRead(zippedFilePath));

    ZipEntry theEntry;
    while ((theEntry = s.GetNextEntry()) != null)
    {
        using (var streamWriter = File.Create(@"D:\extractedXML.xml"))
        {
            var size = 2048;
            var data = new byte[size];
            while (true)
            {
                size = s.Read(data, 0, size);
                if (size > 0)
                {
                    streamWriter.Write(data, 0, size);
                }
                else
                {
                    break;
                }
            }
            streamWriter.Close();
        }
    }
    s.Close();
}

推荐答案

试一下:

var response = webRequest.GetResponse() as HttpWebResponse;
var stream = response.GetResponseStream();
var s = new ZipInputStream(stream);

我相信您已经非常接近并且您使用的是正确的方法-您可以使用

I believe you're very close and that you're using the right approach -- you can use this article to back that up -- their code is very similar.

这篇关于如何提取从HttpWebResponse收到的压缩文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆