3

I am trying to GZIP some XML that gets streamed over HTTP (not a web service)

if (ZipOutput)
{
    output = new GZipStream(Context.Response.OutputStream, CompressionMode.Compress);
    Context.Response.AppendHeader("Content-Encoding", "gzip");
}
else
{
    output = Context.Response.OutputStream;
}

EscapeXMLTextWriter xmlWriter = new EscapeXMLTextWriter(output, new UTF8Encoding())
{
    Formatting = Formatting.Indented
};

ZipOutput is true, but the response doesn't seem to be zipped. Any pointers, alternative techniques?

I know I could use SharpZipLib, but wanted to use the GZipStream class in the framework.

Bjarke Freund-Hansen
  • 23,930
  • 22
  • 87
  • 133
Matt Evans
  • 6,601
  • 7
  • 29
  • 60
  • A guess: maybe the browser "helpfully" unzips the response? Try [Fiddler](http://www.fiddler2.com/fiddler2/) to really see the response. – Hans Kesting Jul 14 '11 at 12:38
  • I don't think so, I'm using firebug to check responses and the response size is identical whether I pass in the zip switch or not. Strangely it works locally. Compression of static and dynamic content is enabled in IIS. It looks like there may be some additional manual configuration to enable compression though.http://stackoverflow.com/questions/702124/enable-iis7-gzip. I haven't made any of these changes to my local IIS installation, so unsure why it would locally and not live. – Matt Evans Jul 15 '11 at 08:53

2 Answers2

3

It looks to me like your code should work - maybe step through it with a debugger to check that the code is really being called as you think it is.

There's a good article on this (including one alternative technique) at - http://www.west-wind.com/weblog/posts/2007/Feb/05/More-on-GZip-compression-with-ASPNET-Content

Stuart
  • 65,726
  • 7
  • 109
  • 161
  • Thanks I will review and post back. – Matt Evans Jul 14 '11 at 11:15
  • I think I sourced my original code from Rick's article. I have stepped through the code locally, and it runs as expected, nonetheless, no compression. See my comments above which suggest there is some addtional configuraiton required to enable Gzip compression on IIS 7 – Matt Evans Jul 15 '11 at 08:58
  • 2
    This shouldn't be anything to do with IIS - you're implementing your own compression, not using the IIS modules/handlers here. It might be worth using fiddler to see what is being received by the browser at the http level. I really think this should be working (so I share your frustration!). Is there a public URL anyone can look at for this? – Stuart Jul 15 '11 at 09:34
  • Would fiddler give me any more information than Firebug? I have tried in Firebug and don't see the Content-Encoding header, and the reported file size is the same "with" or without the compression. – Matt Evans Jul 18 '11 at 11:23
  • 1
    In answer to your question regarding a public URL there is one, but unfortunately I can't disclose it, since the feed data is sensitive. – Matt Evans Jul 18 '11 at 11:39
  • I don't know if Fiddler would give you more information, but it would certainly guarantee that you are looking at the HTTP level traffic – Stuart Jul 18 '11 at 12:59
2

I figured this issue out I think - it turned out the office firewall was stripping the Accept-Encoding header on outbound requests.

Matt Evans
  • 6,601
  • 7
  • 29
  • 60
  • I don't quite understand how this fixed your problem - I thought you were sure that `if (ZipOutput)` was evaluating to true? – Stuart Jul 19 '11 at 09:39
  • @Stuart - sorry - the code sample doesn't make it clear, but ZipOutput isn't a flag to detect the Accept-Encoding header but rather a wrapper for an optional querystring parameter (&zip=1). In retrospect, it needs to check for the header too. – Matt Evans Jul 20 '11 at 08:50
  • Ah - I see - so the bit in your question that says "ZipOutput is true" is wrong. Thanks – Stuart Jul 20 '11 at 14:19