C# .NET - compress query string with special characters.

Asked By rrd on 10-Mar-10 01:42 AM
Hi All,

I am trying to compress the string & pass it to the next page using query string. the string can contain the special characters like [ À Á Â Ã Ä Å Æ Ç È Ê É Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö 󿙙 Ù Ú Û Ü Ý Ÿ Þ Š Œ § ¿ ¥ ß à á â ã ä  𑥈 ç è é ê ÿ þ š œ ¢ £ ë ì í î ï ð ñ ò ó ô õ ö ø ù ú û ü ý ÿ þ š]

for eg. - String can be "dear œ¢£,  how are you," Then the output will be " Untitled Page dear œ¢£, how are y"

I am using Encoding.UTF8....

can any one please let me know that why compress() not returning whole string?
or is there any other way to achive this?
Code is here

private static string Compress(string data)
            using (MemoryStream ms = new MemoryStream())
                using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
                    zip.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
                return Convert.ToBase64String(ms.ToArray());
Sandra Jain replied to rrd on 10-Mar-10 01:47 AM
try this:

  1. function compress(data){
  2.     var q = {}, ret = "";
  3.     data.replace(/([^=&]+)=([^&]*)/g, function(m, key, value){
  4.         q[key] = (q[key] ? q[key] + "," : "") + value;
  5.     });
  6.     for ( var key in q )
  7.         ret = (ret ? ret + "&" : "") + key + "=" + q[key];
  8.     return ret;
  9. }


rrd replied to Sandra Jain on 10-Mar-10 01:53 AM
not working
also javascript will not going to work in my case
Hesham Amin replied to rrd on 10-Mar-10 02:58 AM
I think what you'll gain by compression, will be lost by encoding. base64 encoding increases the size by about 1/3.
Also note that base 64 encoding produces + and / characters, which are not allowed in query strings. You'll need to encode it again or replace these characters.
rrd replied to Hesham Amin on 10-Mar-10 06:22 AM
Hi Hesham,
I agree what you are saying
can you tell me what is exact problem or any limitation of UTF-8 or base64
can you suggest me any other solution for this because the issue is not with the character limitation or size