同时我们还可以删除一些空白
段,空行,注释等以使得HTML文档的尺寸变得更小. 让我们先来实现压缩与删除空白类, 继承自Stream类:
///
/// CompressWhitespaceFilter
/// public class CompressWhitespaceFilter : Stream
{
private GZipStream _contentGZipStream;
private DeflateStream _content_DeflateStream;
private Stream _contentStream;
private CompressOptions _compressOptions;
///
/// Initializes a new instance of the class.
/// ///
The content stream.
///
The compress options.
public CompressWhitespaceFilter(Stream contentStream, CompressOptions compressOptions)
{
if (compressOptiOns== CompressOptions.GZip)
{
this._cOntentGZipStream= new GZipStream(contentStream, CompressionMode.Compress);
this._cOntentStream= this._contentGZipStream;
}
else if (compressOptiOns== CompressOptions.Deflate)
{
this._content_DeflateStream = new DeflateStream(contentStream,CompressionMode.Compress);
this._cOntentStream= this._content_DeflateStream;
}
else
{
this._cOntentStream= contentStream;
}
this._compressOptiOns= compressOptions;
}
public override bool CanRead
{
get { return this._contentStream.CanRead; }
}
public override bool CanSeek
{
get { return this._contentStream.CanSeek; }
}
public override bool CanWrite
{
get { return this._contentStream.CanWrite; }
}
public override void Flush()
{
this._contentStream.Flush();
}
public override long Length
{
get { return this._contentStream.Length; }
}
public override long Position
{
get
{
return this._contentStream.Position;
}
set
{
this._contentStream.Position = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
return this._contentStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return this._contentStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
this._contentStream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
byte[] data = new byte[count + 1];
Buffer.BlockCopy(buffer, offset, data, 0, count);
string strtext = System.Text.Encoding.UTF8.GetString(data);
strtext = Regex.Replace(strtext, "^\\s*", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
strtext = Regex.Replace(strtext, "\\r\\n", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
strtext = Regex.Replace(strtext, "", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
byte[] outdata = System.Text.Encoding.UTF8.GetBytes(strtext);
this._contentStream.Write(outdata, 0, outdata.GetLength(0));
}
}
///
/// CompressOptions
/// ///
///
public enum CompressOptions
{
GZip,
Deflate,
None
}
上面的代码使用正则表达式替换字符串,你可以修改那些正则表达式来满足你的需求. 我们同时使用了GZipStream与DeflateStream实现了压缩. 好的,接下来与
///
/// CompressWhitespaceModule
/// public class CompressWhitespaceModule : IHttpModule
{
#region IHttpModule Members
///
/// Disposes of the resources (other than memory) used by the module that implements .
/// public void Dispose()
{
// Nothing to dispose;
}
///
/// Initializes a module and prepares it to handle requests.
/// ///
An
that provides access to the methods, properties, and events common to all application objects within an ASP.NET application
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
///
/// Handles the BeginRequest event of the context control.
/// ///
The source of the event.
///
The
instance containing the event data.
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app.Request.RawUrl.Contains(".aspx"))
{
HttpContext cOntext= app.Context;
HttpRequest request = context.Request;
string acceptEncoding = request.Headers["Accept-Encoding"];
HttpResponse respOnse= context.Response;
if (!string.IsNullOrEmpty(acceptEncoding))
{
acceptEncoding = acceptEncoding.ToUpperInvariant();
if (acceptEncoding.Contains("GZIP"))
{
response.Filter = new CompressWhitespaceFilter(context.Response.Filter, CompressOptions.GZip);
response.AppendHeader("Content-encoding", "gzip");
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.Filter = new CompressWhitespaceFilter(context.Response.Filter, CompressOptions.Deflate);
response.AppendHeader("Content-encoding", "deflate");
}
}
response.Cache.VaryByHeaders["Accept-Encoding"] = true;
}
}
#endregion
}
很简单,你可以按需求来增加更多的功能. 希望对您开发有帮助.
作者:Petter Liu