Question : How to compress a string into a short representation in ASP.NET using C# or VB.NET?

We have a need to compress a string into a representation that is shorter than the original string then to decompress it.  I wanted to know if there is a way to do this in .NET for strings that are about 200-500 characters (not extremely long, but we need to compress them for use in a URL/querystring).

Answer : How to compress a string into a short representation in ASP.NET using C# or VB.NET?

Because these compression methods result in binary data, and you can't pass bytes in a URL, the compressed data needs to be encoded as a Base64 string. While the compression might reduce the size of the string, I suspect the Base64 encoding is likely to get you a string larger than the one you were trying to compress in the first place - particularly if your string is largely random.

Small data sets, e.g. a 500-byte string, tend to not compress well and often result in an increase in size.

What is contained in your string?

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			string longString = GetLongString(500);
			
			string compressed = StringCompressor.CompressString(longString);
			string uncompressed = StringCompressor.DecompressString(compressed);

			Console.WriteLine("Original string length: {0}\r\nCompressed string length: {1}",
				longString.Length, compressed.Length);
			Console.ReadKey();
		}

		static string GetLongString(int len)
		{
			StringBuilder str = new StringBuilder();
			Random rand = new Random(DateTime.Now.Millisecond);

			for (int i = 0; i < len; i++)
				str.Append((char)rand.Next(65, 91));

			return str.ToString();
		}
	}

	public static class StringCompressor
	{
		public static string CompressString(string Input)
		{
			byte[] inputBytes = Encoding.ASCII.GetBytes(Input);
			byte[] outputBytes;

			using (MemoryStream outputStream = new MemoryStream())
			{
				using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress, true))
				{
					compressionStream.Write(inputBytes, 0, inputBytes.Length);
				}
				outputBytes = new byte[outputStream.Length];
				outputStream.Position = 0;
				outputStream.Read(outputBytes, 0, outputBytes.Length);
			}

			return Convert.ToBase64String(outputBytes);
		}

		public static string DecompressString(string Input)
		{
			byte[] inputBytes = Convert.FromBase64String(Input);
			byte[] outputBytes;


			using (MemoryStream inputStream = new MemoryStream())
			{
				inputStream.Write(inputBytes, 0, inputBytes.Length);
				inputStream.Position = 0;
				using (GZipStream outputStream = new GZipStream(inputStream, CompressionMode.Decompress))
				{
					outputBytes = GetAllBytes(outputStream);
				}
			}
			return Encoding.ASCII.GetString(outputBytes);
		}

		static byte[] GetAllBytes(Stream stream)
		{
			const int buffer_size = 1024;
			byte[] buffer = new byte[buffer_size];
			List<byte> streamBytes = new List<byte>();

			if (stream.CanSeek)
				stream.Position = 0;

			int bytesRead = 0;
			do
			{
				bytesRead = stream.Read(buffer, 0, buffer_size);
				if (bytesRead > 0)
					streamBytes.AddRange(buffer.Take(bytesRead));
			} while (bytesRead > 0);

			return streamBytes.ToArray();
		}
	}
}
Random Solutions  
 
programming4us programming4us