ASP.NET Tutorial/File Directory/GZipStream

Материал из .Net Framework эксперт
Версия от 11:57, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Compressing a file using GZipStream (C#)

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.IO.rupression" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        string filename = Server.MapPath("Data.txt");
        FileStream infile = File.OpenRead(filename);
        byte[] buffer = new byte[infile.Length];
        infile.Read(buffer, 0, buffer.Length);
        infile.Close();
        FileStream outfile = File.Create(Path.ChangeExtension(filename, "zip"));
        GZipStream gzipStream = new GZipStream(outfile, CompressionMode.rupress);
        gzipStream.Write(buffer, 0, buffer.Length);
        gzipStream.Close();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>


Compressing a file using GZipStream (VB)

<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.IO.rupression" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim filename As String = Server.MapPath("TextFile.txt")
        Dim infile As FileStream = File.OpenRead(filename)
        Dim buffer(infile.Length) As Byte
        infile.Read(buffer, 0, buffer.Length)
        infile.Close()
        Dim outfile As System.IO.FileStream = File.Create(Path.ChangeExtension(filename, "zip"))
        Dim gzipStream As New GZipStream(outfile, CompressionMode.rupress)
        gzipStream.Write(buffer, 0, buffer.Length)
        gzipStream.Close()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>


Compressing HTTP output with an HttpModule (C#)

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.IO.rupression;
namespace ClassLibrary1
{
    public class Class1 : IHttpModule
    {
        void IHttpModule.Dispose()
        {
            throw new Exception("The method or operation is not implemented.");
        }
        void IHttpModule.Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            string encodings = app.Request.Headers.Get("Accept-Encoding");
            if (encodings == null)
                return;
            Stream s = app.Response.Filter;
            encodings = encodings.ToLower();
            if (encodings.Contains("gzip"))
            {
                app.Response.Filter = new GZipStream(s, CompressionMode.rupress);
                app.Response.AppendHeader("Content-Encoding", "gzip");
                app.Context.Trace.Warn("GZIP Compression on");
            }
            else
            {
                app.Response.Filter =
                              new DeflateStream(s, CompressionMode.rupress);
                app.Response.AppendHeader("Content-Encoding", "deflate");
                app.Context.Trace.Warn("Deflate Compression on");
            }
        }
    }
}


Compressing HTTP output with an HttpModule (VB)

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Web
Imports System.IO
Imports System.IO.rupression
Public Class Class1
    Implements IHttpModule
    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
        Throw New Exception("The method or operation is not implemented.")
    End Sub
    Public Sub Init(ByVal context As System.Web.HttpApplication) _
       Implements System.Web.IHttpModule.Init
        AddHandler context.BeginRequest, AddressOf context_BeginRequest
    End Sub
    Public Sub context_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim app As HttpApplication = CType(sender, HttpApplication)
        Dim encodings As String = app.Request.Headers.Get("Accept-Encoding")
        If (encodings = Nothing) Then
            Return
        End If
        Dim s As Stream = app.Response.Filter
        encodings = encodings.ToLower()
        If (encodings.Contains("gzip")) Then
            app.Response.Filter = New GZipStream(s, CompressionMode.rupress)
            app.Response.AppendHeader("Content-Encoding", "gzip")
            app.Context.Trace.Warn("GZIP Compression on")
        Else
            app.Response.Filter = _
                          New DeflateStream(s, CompressionMode.rupress)
            app.Response.AppendHeader("Content-Encoding", "deflate")
            app.Context.Trace.Warn("Deflate Compression on")
        End If
    End Sub
End Class