Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1047005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:14:10+00:00 2026-05-16T16:14:10+00:00

I have created an extension method called AddGZip which looks like the following: public

  • 0

I have created an extension method called AddGZip which looks like the following:

public static void AddGZip(this HttpResponse response)
{
    response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
    response.AppendHeader("Content-Encoding", "gzip");
}

This is a very cut down version of the code:

var response = HttpContext.Current.Response;
var request = HttpContext.Current.Request;
var result = File.ReadAllText(path);
if (request.SupportsGZip)
{
  response.AddGZip();
}
response.Write(result);
response.Flush();

When you view the response in a web browser with GZip support you get an error like this:

“XML Parsing Error: unclosed token
Location: http://webserver1/1234.xml
Line Number 78, Column 1:”

When i view the source it’s basically missed out the last > from the end of the XML file. So 1 or 2 bytes.

If I comment out the AddGZip Line it works fine. However I really want to support GZip as the XML can be quite large.

Does anyone have a suggestion for me? I’ve tried checking lots of blogs but no solution seems to be out there for this type of error.

Dave

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:14:11+00:00Added an answer on May 16, 2026 at 4:14 pm

    There is an issue (or perhaps a really clever feature that I haven’t seen justified anywhere) with DeflateStream (GZipStream builds on DeflateStream and inherits the issue*), where flushing can lose data.

    Response.Flush() will flush the filter. The solution is to use a wrapper that is aware of both the zipping and the underlying sink, and only flushes the latter:

    public enum CompressionType
    {
        Deflate,
        GZip
    }
    /// <summary>
    /// Provides GZip or Deflate compression, with further handling for the fact that
    /// .NETs GZip and Deflate filters don't play nicely with chunked encoding (when
    /// Response.Flush() is called or buffering is off.
    /// </summary>
    public class WebCompressionFilter : Stream
    {
        private Stream _compSink;
        private Stream _finalSink;
        public WebCompressionFilter(Stream stm, CompressionType comp)
        {
            switch(comp)
            {
                case CompressionType.Deflate:
                    _compSink = new DeflateStream((_finalSink = stm), CompressionMode.Compress);
                    break;
                case CompressionType.GZip:
                    _compSink = new GZipStream((_finalSink = stm), CompressionMode.Compress);
                    break;
            }
        }
        public override bool CanRead
        {
            get
            {
                return false;
            }
        }
        public override bool CanSeek
        {
            get
            {
                return false;
            }
        }
        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }
        public override long Length
        {
            get
            {
                throw new NotSupportedException();
            }
        }
        public override long Position
        {
            get
            {
                throw new NotSupportedException();
            }
            set
            {
                throw new NotSupportedException();
            }
        }
        public override void Flush()
        {
            //We do not flush the compression stream. At best this does nothing, at worse it
            //loses a few bytes. We do however flush the underlying stream to send bytes down the
            //wire.
            _finalSink.Flush();
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotSupportedException();
        }
        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            _compSink.Write(buffer, offset, count);
        }
        public override void WriteByte(byte value)
        {
            _compSink.WriteByte(value);
        }
        public override void Close()
        {
            _compSink.Close();
            _finalSink.Close();
            base.Close();
        }
        protected override void Dispose(bool disposing)
        {
            if(disposing)
            {
                _compSink.Dispose();
                _finalSink.Dispose();
            }
            base.Dispose(disposing);
        }
    }
    

    It’s also worth noting that most user-agents that support gzip-encoding also support deflate-encoding. While the size improvement with deflate is negliable (literally a few bytes), some libraries on some architecture deals with deflate considerably better (this goes for both compressing and decompressing), so it’s always worth favouring deflate over gzip with HTTP compression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method: public static TEventInvocatorParameters Until <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p, Func<TEventArgs,
I am having an issue using BinaryFormatter.Serialize. I have this generic extension method to
Say I have a method called GetCatsByColor which takes a color as a string,
I have created the app with forge, and have added in the chrome extension,
i have installed SharePoint 2007 and VS2008 extension on my computer.i have created a
I have an app that recognizes an extension of a file created in my
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I'm having trouble getting the C# compiler to call an extension method I created,
I need to use a SoapExtension subclass (which I have created), but it seems
I have a common project called common. Inside is a class called ExtensionMethods which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.