Click here to Skip to main content
12,069,536 members (63,406 online)
Rate this:
 
Please Sign up or sign in to vote.
hi all
i have one function which generates string
that string is very lengthy.
i want to decrease its length and size

this is my code and result:
not posting real string it is very long
protected void Page_Load(object sender, EventArgs e)
    {
        string txt = "somestring";
        Label1.Text = txt.Length.ToString();
        string cmpString = Compress(txt);
        Label2.Text = cmpString.Length.ToString();
    }
 
//For Compression
    public string Compress(string text)
            {
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            MemoryStream ms = new MemoryStream();
            using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
            {
            zip.Write(buffer, 0, buffer.Length);
            }
            ms.Position = 0;
            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);
            byte[] gzBuffer = new byte[compressed.Length + 4];
            System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
            System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
            return Convert.ToBase64String(gzBuffer);
    
    }

result:
465832
667296



my question is when i m compressing the data the length of that string should reduce it is increasing
is there any solution becoz i want to minimize its length and size.
Posted 2-Aug-12 1:58am
MAU7871.6K
Edited 2-Aug-12 2:14am
_Amy55K
v3
Comments
Vani Kulkarni 2-Aug-12 6:59am
   
What is the question?
MAU787 2-Aug-12 7:02am
   
my question is when i m compressing the data the length of that string should reduce it is increasing
is there any solution becoz i want to minimize its length and size
barneyman 2-Aug-12 7:12am
   
are you forgetting that b64 encoding adds 33% ?
MAU787 2-Aug-12 7:14am
   
ohh really?
so what shud i do for reducing its size??

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,
You are converting your string to byte and again displaying it in string format?
The string representation of byte will be big in length and size anyway. Store it in byte only.


--Amit
  Permalink  
Comments
MAU787 2-Aug-12 7:17am
   
thanx for reply

do you have reference code for it??
_Amy 2-Aug-12 7:22am
   
You should try Google[^].
MAU787 2-Aug-12 7:26am
   
thanx
but i wanted to pass string only...i want to reduce length of that string while passing.
barneyman 2-Aug-12 8:17am
   
your requirements are less than explicit - but, maybe you want to look at passing by reference?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 2 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100