生成并提供在ASP.NET中压缩的gz

时间:2009-11-13 16:07:56

标签: c# asp.net gz

您好我需要从ASHX GZ压缩文件提供服务。在代码中我已经明确了字符串:

public void ProcessRequest(HttpContext context)
{
    // this is the code without compression
    HttpRequest Request = context.Request;
    HttpResponse Response = context.Response;

    Response.ContentEncoding = Encoding.UTF8;
    Response.ContentType = "text/xml";

    // this is the string to compress and send to the client
    string xml = GenerateXml();

    Response.Write(output);
    Response.End();
}

现在我需要

任何帮助?

2 个答案:

答案 0 :(得分:4)

您可以在IIS级别enable compression获取特定目录。我相信这比在通用处理程序中手动执行更有效。


更新:

您可以使用GZipStream将xml直接压缩到响应流:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "application/x-gzip";
    var xml = "<xml/>";
    using (var gzipStream = new GZipStream(context.Response.OutputStream, CompressionMode.Compress))
    {
        var buffer = Encoding.UTF8.GetBytes(xml);
        gzipStream.Write(buffer, 0, buffer.Length);
    }
}

答案 1 :(得分:0)

ASP.NET支持GZip压缩。一个快速的谷歌导致这些帖子似乎有所帮助:

http://west-wind.com/WebLog/posts/10294.aspx - 做GZip
http://www.west-wind.com/Weblog/posts/10564.aspx - 返回结果