Saturday, 3 October 2015

Compress and decompress stream object in asp.net - Deflate

Leave a Comment
This is a snippet code to provides methods and properties for compressing and decompressing streams using the Deflate 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,  
   }  
 }  


DeflateStream

  using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 namespace CompressionMethod  
 {  
   /// <summary>  
   /// Provides methods and properties for compressing and decompressing streams using the Deflate algorithm.  
   /// </summary>  
   public class DeflateStream : Stream  
   {  
     #region "Variables"  
     private System.IO.Compression.DeflateStream mSystemStream;  
     #endregion  
     #region "Constructors"  
     /// <summary>  
     /// Initializes a new instance with an expandable capacity initialized to zero.  
     /// </summary>  
     protected DeflateStream(System.IO.Compression.DeflateStream ds)  
     {  
       mSystemStream = ds;  
     }  
     #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 DeflateStream New(Stream s, CompressionMode compression, bool leaveOpen)  
     {  
       System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(s.SystemStream, (System.IO.Compression.CompressionMode)compression, leaveOpen);  
       return new DeflateStream(ds);  
     }  
     /// <summary>  
     /// Returns new instance of stream reader class.  
     /// </summary>     
     /// <param name="s">Stream object</param>  
     /// <param name="compression">Compression mode</param>  
     public static DeflateStream New(Stream s, CompressionMode compression)  
     {  
       System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(s.SystemStream, (System.IO.Compression.CompressionMode)compression);  
       return new DeflateStream(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 "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  
   }  
 }  





By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.