Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to compress byte array data to reduce its size for sending to another machine using socket programming.For that I used different codes which are got from net.But the result is opposite.ie increased the size of compressed byte array .If u dont mind plz help me to solve this problem in C#.net windows application.
Posted
Comments
OriginalGriff 30-Oct-10 4:40am    
Without seeing teh relevant code, it is not possible to comment.

Example:
using System.IO;
using System.IO.Compression;

public static byte[] Compress(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream();
            GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
            zip.Write(buffer, 0, buffer.Length);
            zip.Close();
            ms.Position = 0;

            MemoryStream outStream = new MemoryStream();

            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);

            byte[] gzBuffer = new byte[compressed.Length + 4];
            Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
            Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
            return gzBuffer;
        }

        public static byte[] Decompress(byte[] gzBuffer)
        {
            MemoryStream ms = new MemoryStream();
            int msgLength = BitConverter.ToInt32(gzBuffer, 0);
            ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

            byte[] buffer = new byte[msgLength];

            ms.Position = 0;
            GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);
            zip.Read(buffer, 0, buffer.Length);

            return buffer;
        }


Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
shoni k 1-Nov-10 0:37am    
by using this code the size of data after compression is too larger than before compression
thanks
Go through the below link This may helps you

http://www.hackchina.com/en/cont/31340[^]

The Above link contains the example code for BCD Compression...
 
Share this answer
 

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