Compress and decompress stream object in asp.net - GZip

This is a snippet code to provides methods and properties for compressing and decompressing streams using the GZip algorithm.

abstract class Stream


  using System;  
using System.IO;
namespace CompressionMethod
{
/// <summary>
/// Provides a generic view of a sequence of bytes.
/// </summary>
public abstract class Stream : IDisposable
{
#region "Properties"
/// <summary>
/// Gets or sets system stream.
/// </summary>
public abstract System.IO.Stream SystemStream
{
get;
}
/// <summary>
/// Gets the length in bytes of the stream.
/// </summary>
public abstract long Length
{
get;
}
/// <summary>
/// Gets or sets the position within the current stream.
/// </summary>
public abstract long Position
{
get;
set;
}
#endregion
#region "Methods"
/// <summary>
/// Sets the position within the current stream to the specified value.
/// </summary>
/// <param name="offset">Offset</param>
/// <param name="loc">Location</param>
public abstract long Seek(long offset, SeekOrigin loc);
/// <summary>
/// Reads from stream.
/// </summary>
/// <param name="array">Array where result is stored</param>
/// <param name="offset">Offset from beginning of file</param>
/// <param name="count">Number of characters which are read</param>
public abstract int Read(byte[] array, int offset, int count);
/// <summary>
/// Releases all resources.
/// </summary>
public abstract void Dispose();
/// <summary>
/// Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream.
/// </summary>
public abstract void Close();
/// <summary>
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
/// </summary>
/// <param name="buffer">Buffer</param>
/// <param name="offset">Offset</param>
/// <param name="count">Number of chars</param>
public abstract void Write(byte[] buffer, int offset, int count);
/// <summary>
/// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
/// </summary>
public abstract void Flush();
/// <summary>
/// Sets length to stream.
/// </summary>
/// <param name="value">Value to set</param>
public abstract void SetLength(long value);
/// <summary>
/// Writes byte to stream.
/// </summary>
/// <param name="value">Value to write</param>
public abstract void WriteByte(byte value);
#endregion
}
}



CompressionMode


  using System;  
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CompressionMethod
{
/// <summary>
/// Specifies whether to compress or decompress the underlying stream.
/// </summary>
public enum CompressionMode
{
/// <summary>
/// Decompresses the underlying stream.
/// </summary>
Decompress = 0,
/// <summary>
/// Compresses the underlying stream.
/// </summary>
Compress = 1,
}
}


GZipStream


  namespace CompressionMethod  
{
/// <summary>
/// Provides methods and properties for compressing and decompressing streams using the GZip algorithm.
/// </summary>
public class GZipStream : Stream
{
#region "Variables"
private System.IO.Compression.GZipStream mSystemStream;
#endregion
#region "Constructors"
/// <summary>
/// Initializes a new instance with an expandable capacity initialized to zero.
/// </summary>
protected GZipStream(System.IO.Compression.GZipStream ds)
{
mSystemStream = ds;
}
#endregion
#region "Public properties"
/// <summary>
/// Gets or sets System.IO stream object.
/// </summary>
public override System.IO.Stream SystemStream
{
get
{
return mSystemStream;
}
}
/// <summary>
/// Gets the length in bytes of the stream.
/// </summary>
public override long Length
{
get
{
return mSystemStream.Length;
}
}
/// <summary>
/// Position of stream.
/// </summary>
public override long Position
{
get
{
return mSystemStream.Position;
}
set
{
mSystemStream.Position = value;
}
}
#endregion
#region "Methods for creating new instance"
/// <summary>
/// Returns new instance of stream reader class.
/// </summary>
/// <param name="s">Stream object</param>
/// <param name="compression">Compression mode</param>
/// <param name="leaveOpen">Whether leave stream open</param>
public static GZipStream New(Stream s, CompressionMode compression, bool leaveOpen)
{
System.IO.Compression.GZipStream ds = new System.IO.Compression.GZipStream(s.SystemStream, (System.IO.Compression.CompressionMode)compression, leaveOpen);
return new GZipStream(ds);
}
/// <summary>
/// Returns new instance of stream reader class.
/// </summary>
/// <param name="s">Stream object</param>
/// <param name="compression">Compression mode</param>
public static GZipStream New(Stream s, CompressionMode compression)
{
System.IO.Compression.GZipStream ds = new System.IO.Compression.GZipStream(s.SystemStream, (System.IO.Compression.CompressionMode)compression);
return new GZipStream(ds);
}
#endregion
#region "Public methods"
/// <summary>
/// Sets the position within the current stream to the specified value.
/// </summary>
/// <param name="offset">Offset</param>
/// <param name="loc">Location</param>
public override long Seek(long offset, System.IO.SeekOrigin loc)
{
return mSystemStream.Seek(offset, (System.IO.SeekOrigin)loc);
}
/// <summary>
/// Reads from stream.
/// </summary>
/// <param name="array">Array where result is stored</param>
/// <param name="offset">Offset from beginning of file</param>
/// <param name="count">Number of characters which are read</param>
public override int Read(byte[] array, int offset, int count)
{
return mSystemStream.Read(array, offset, count);
}
/// <summary>
/// Releases all resources.
/// </summary>
public override void Dispose()
{
mSystemStream.Dispose();
}
/// <summary>
/// Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream.
/// </summary>
public override void Close()
{
mSystemStream.Close();
}
/// <summary>
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
/// </summary>
/// <param name="buffer">Buffer</param>
/// <param name="offset">Offset</param>
/// <param name="count">Number of chars</param>
public override void Write(byte[] buffer, int offset, int count)
{
mSystemStream.Write(buffer, offset, count);
}
/// <summary>
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
/// </summary>
public override void Flush()
{
mSystemStream.Flush();
}
/// <summary>
/// Sets length to stream.
/// </summary>
/// <param name="value">Value to set</param>
public override void SetLength(long value)
{
mSystemStream.SetLength(value);
}
/// <summary>
/// Writes byte to stream.
/// </summary>
/// <param name="value">Value to write</param>
public override void WriteByte(byte value)
{
mSystemStream.WriteByte(value);
}
#endregion
}
}


Popular posts from this blog

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example