Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

MemoryStream Compression

, 20 Jun 2004
MemoryStream based compression based on SharpZipLib.
mscomptest.zip
MSCompTest
bin
App.ico
MSCompTest.csproj.user
ICSharpCode.SharpZipLib.dll
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.BZip2;

namespace MSCompTest
{
	/// <summary>
	/// Summary description for MainForm.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txtRawData;
		private System.Windows.Forms.Label lblRawData;
		private System.Windows.Forms.TextBox txtOutput;
		private System.Windows.Forms.Label lblOutput;
		private System.Windows.Forms.Button cmdCompress;
		private System.Windows.Forms.Button cmdUncompress;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		/// 
		private System.ComponentModel.Container components = null;

		// Compression/Decompression objects
		private BZip2 m_bzip2Manager = new BZip2();
		private BZip2InputStream m_isBZip2 = null;
		private BZip2OutputStream m_osBZip2 = null;
		private System.Windows.Forms.Label lblRawDataSize;
		private System.Windows.Forms.Label lblOutputSize;
		private MemoryStream m_msBZip2 = null;

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.txtRawData = new System.Windows.Forms.TextBox();
			this.lblRawData = new System.Windows.Forms.Label();
			this.txtOutput = new System.Windows.Forms.TextBox();
			this.lblOutput = new System.Windows.Forms.Label();
			this.cmdCompress = new System.Windows.Forms.Button();
			this.cmdUncompress = new System.Windows.Forms.Button();
			this.lblRawDataSize = new System.Windows.Forms.Label();
			this.lblOutputSize = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// txtRawData
			// 
			this.txtRawData.Location = new System.Drawing.Point(0, 32);
			this.txtRawData.Multiline = true;
			this.txtRawData.Name = "txtRawData";
			this.txtRawData.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txtRawData.Size = new System.Drawing.Size(336, 104);
			this.txtRawData.TabIndex = 0;
			this.txtRawData.Text = "";
			this.txtRawData.TextChanged += new System.EventHandler(this.txtRawData_TextChanged);
			// 
			// lblRawData
			// 
			this.lblRawData.Location = new System.Drawing.Point(0, 0);
			this.lblRawData.Name = "lblRawData";
			this.lblRawData.TabIndex = 1;
			this.lblRawData.Text = "RAW DATA:";
			// 
			// txtOutput
			// 
			this.txtOutput.Location = new System.Drawing.Point(456, 32);
			this.txtOutput.Multiline = true;
			this.txtOutput.Name = "txtOutput";
			this.txtOutput.ReadOnly = true;
			this.txtOutput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.txtOutput.Size = new System.Drawing.Size(336, 104);
			this.txtOutput.TabIndex = 3;
			this.txtOutput.Text = "";
			this.txtOutput.TextChanged += new System.EventHandler(this.txtOutput_TextChanged);
			// 
			// lblOutput
			// 
			this.lblOutput.Location = new System.Drawing.Point(456, 0);
			this.lblOutput.Name = "lblOutput";
			this.lblOutput.TabIndex = 3;
			this.lblOutput.Text = "OUTPUT:";
			// 
			// cmdCompress
			// 
			this.cmdCompress.Location = new System.Drawing.Point(344, 32);
			this.cmdCompress.Name = "cmdCompress";
			this.cmdCompress.Size = new System.Drawing.Size(104, 23);
			this.cmdCompress.TabIndex = 1;
			this.cmdCompress.Text = "Compress  --->";
			this.cmdCompress.Click += new System.EventHandler(this.cmdCompress_Click);
			// 
			// cmdUncompress
			// 
			this.cmdUncompress.Location = new System.Drawing.Point(344, 112);
			this.cmdUncompress.Name = "cmdUncompress";
			this.cmdUncompress.Size = new System.Drawing.Size(104, 23);
			this.cmdUncompress.TabIndex = 2;
			this.cmdUncompress.Text = "<--- Uncompress";
			this.cmdUncompress.Click += new System.EventHandler(this.cmdUncompress_Click);
			// 
			// lblRawDataSize
			// 
			this.lblRawDataSize.Location = new System.Drawing.Point(232, 8);
			this.lblRawDataSize.Name = "lblRawDataSize";
			this.lblRawDataSize.TabIndex = 6;
			this.lblRawDataSize.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// lblOutputSize
			// 
			this.lblOutputSize.Location = new System.Drawing.Point(688, 8);
			this.lblOutputSize.Name = "lblOutputSize";
			this.lblOutputSize.TabIndex = 7;
			this.lblOutputSize.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
			this.ClientSize = new System.Drawing.Size(792, 141);
			this.Controls.Add(this.lblOutputSize);
			this.Controls.Add(this.lblRawDataSize);
			this.Controls.Add(this.cmdUncompress);
			this.Controls.Add(this.cmdCompress);
			this.Controls.Add(this.txtOutput);
			this.Controls.Add(this.lblOutput);
			this.Controls.Add(this.txtRawData);
			this.Controls.Add(this.lblRawData);
			this.Font = new System.Drawing.Font("Comic Sans MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "MainForm";
			this.Text = "Memory Stream Compression Test";
			this.Load += new System.EventHandler(this.MainForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{
			lblRawDataSize.Text = "Empty";
			lblOutputSize.Text = "Empty";
		}

		private void cmdCompress_Click(object sender, System.EventArgs e)
		{
			if(txtRawData.Text.Length < 8)
			{
				MessageBox.Show("You must have something in the output field to decompress.", "Insufficient Output", MessageBoxButtons.OK, MessageBoxIcon.Error);
				txtRawData.Focus();
				txtRawData.SelectAll();
			}
			else
			{
				m_msBZip2 = new MemoryStream();
				m_osBZip2 = new BZip2OutputStream(m_msBZip2);
				m_osBZip2.Write(Encoding.ASCII.GetBytes(txtRawData.Text), 0, txtRawData.Text.Length);
				m_osBZip2.Finalize();
				m_osBZip2.Close();
				txtOutput.Text = Convert.ToBase64String(m_msBZip2.ToArray());
				m_msBZip2.Close();
				m_osBZip2 = null;
				m_msBZip2 = null;
			}
		}

		private void cmdUncompress_Click(object sender, System.EventArgs e)
		{
			if(txtOutput.Text.Length < 8)
				MessageBox.Show("You must enter at least 8 bytes of data into the RAW DATA field to compress.", "Insufficient RAW DATA", MessageBoxButtons.OK, MessageBoxIcon.Error);
			else
			{
				m_msBZip2 = new MemoryStream(Convert.FromBase64String(txtOutput.Text));
				m_isBZip2 = new BZip2InputStream(m_msBZip2);
				byte[] bytesUncompressed = new byte[m_isBZip2.Length];
				m_isBZip2.Read(bytesUncompressed, 0, (int)m_isBZip2.Length);
				m_isBZip2.Close();
				m_msBZip2.Close();
				txtRawData.Text = Encoding.ASCII.GetString(bytesUncompressed);
				m_isBZip2 = null;
				m_msBZip2 = null;
			}
		}

		private void txtRawData_TextChanged(object sender, System.EventArgs e)
		{
			lblRawDataSize.Text = txtRawData.Text.Length.ToString() + " byte(s).";
		}

		private void txtOutput_TextChanged(object sender, System.EventArgs e)
		{
			lblOutputSize.Text = txtOutput.Text.Length.ToString() + " byte(s).";
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Astaelan
Web Developer
Canada Canada
Short and simple, I'm a self contracted programmer, my strongest programming skills are in C/C++ and C#/.NET. I have a nack for porting C algorithms to C#.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 21 Jun 2004
Article Copyright 2004 by Astaelan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid