Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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

What I have tried:

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
Updated 13-Jun-19 20:29pm
v4
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??

That's not the compression, that's Base64.
Base64 takes a string of byte values (which can hold 256 different values) and converts it to a "text friendly" version that can be transfered via text-only protocols like JSON, XML, and HTML. It does that by storing 6 bits of the first byte in the first output character, the remaining 2 bits plus 4 from the next into the second, the remaining 4 plus 2 from the next in to the third, and the remaining 6 into the fourth.
So for each three bytes in the input stream you get four in the output (plus a little bit of prefix and suffix data).

Even if your compression works well, your conversion to Base64 will expand the output by a quarter!
 
Share this answer
 
Comments
CHill60 14-Jun-19 3:38am    
This is not like you! Check the date of the post :-)
Good post though!
OriginalGriff 14-Jun-19 3:45am    
I was drinking my first coffee - but it came up in the "Questions" list on the front page (and is still in second place).
I'm guessing a spam answer / comment was added which "bumped" it?
CHill60 14-Jun-19 5:33am    
Yeah, probably. It's been raised in Sugs & Bugs before I think
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
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900