代码之家  ›  专栏  ›  技术社区  ›  CraftyFella

GZipStream正在切断XML的最后一部分

  •  4
  • CraftyFella  · 技术社区  · 14 年前

    我创建了一个名为AddGZip的扩展方法,如下所示:

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

    这是一个非常精简的代码版本:

    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();
    

    在支持GZip的web浏览器中查看响应时,会出现如下错误:

    “XML分析错误:未关闭的令牌 地点: http://webserver1/1234.xml 第78行,第1列:“

    >

    如果我注释掉AddGZip行,就可以了。不过,我真的很想支持GZip,因为XML可能非常大。

    有人给我提个建议吗?我试过查看很多博客,但似乎没有解决这类错误的方法。

    戴夫

    2 回复  |  直到 14 年前
        1
  •  7
  •   Jon Hanna    14 年前

    DeflateStream GZipStream 建立在 放气流

    Response.Flush() 会冲洗过滤器。解决方案是使用一个包装器,该包装器同时知道压缩和底层接收器,并且只刷新后者:

    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);
        }
    }
    

        2
  •  0
  •   Community CDub    8 年前

    你试过通过IIS添加gzip吗?有一个 question