tags:

views:

53

answers:

2

I'm trying to compress and decompress a memory stream to send it over an tcp connection. In the following code snap I do do the decompressing right after compressing to get it working first. What ever I do I end up with a devompressed buffer wit all zero's and in the line int read = Decompress.Read(buffie, 0, buffie.Length); it seems that 0 bytes are read.

Does anyone has a clue what is wrong?

bytesRead = ms.Read(buf, 0, i);

MemoryStream partialMs = new MemoryStream();
GZipStream gZip = new GZipStream(partialMs, CompressionMode.Compress);

gZip.Write(buf, 0, buf.Length);

partialMs.Position = 0;

byte[] compressedBuf = new byte[partialMs.Length];

partialMs.Read(compressedBuf, 0, (int)partialMs.Length);

partialMs.Close();

byte[] gzBuffer = new byte[compressedBuf.Length + 4];
System.Buffer.BlockCopy(compressedBuf, 0, gzBuffer, 4, compressedBuf.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buf.Length), 0, gzBuffer, 0, 4);

using (MemoryStream mems = new MemoryStream())
{
    int msgLength = BitConverter.ToInt32(gzBuffer, 0);

    byte[] buffie = new byte[msgLength];
    mems.Write(gzBuffer, 4, gzBuffer.Length - 4);
    mems.Flush();
    mems.Position = 0;

    using (GZipStream Decompress = new GZipStream(mems, CompressionMode.Decompress, true))
    {
        int read = Decompress.Read(buffie, 0, buffie.Length);
        Decompress.Close();
    }
}
+1  A: 

You're not closing the GzipStream you're writing to, so it's probably all buffered. I suggest you close it when you're done writing your data.

By the way, you can get the data out of a MemoryStream much more easily than your current code: use MemoryStream.ToArray.

Jon Skeet
you are right, Jon, and a closer look at the posted code will reveal a few more fundamental problems.
Sky Sanders
+1  A: 

Your implementation could use some work. there seems to be some confusion as to which streams should be used where. here is a working example to get you started..

see user content at the bottom of this MSDN page


var original = new byte[65535];
var compressed = GZipTest.Compress(original);
var decompressed = GZipTest.Decompress(compressed);

using System.IO;
using System.IO.Compression;

public class GZipTest
{
    public static byte[] Compress(byte[] uncompressedBuffer)
    {
        using (var ms = new MemoryStream())
        {
            using (var gzip = new GZipStream(ms, CompressionMode.Compress, true))
            {
                gzip.Write(uncompressedBuffer, 0, uncompressedBuffer.Length);
            }
            byte[] compressedBuffer = ms.ToArray();
            return compressedBuffer;
        }
    }

    public static byte[] Decompress(byte[] compressedBuffer)
    {
        using (var gzip = new GZipStream(new MemoryStream(compressedBuffer), CompressionMode.Decompress))
        {
            byte[] uncompressedBuffer = ReadAllBytes(gzip);
            return uncompressedBuffer;
        }
    }

    private static byte[] ReadAllBytes(Stream stream)
    {
        var buffer = new byte[4096];
        using (var ms = new MemoryStream())
        {
            int bytesRead = 0;
            do
            {
                bytesRead = stream.Read(buffer, 0, buffer.Length);
                if (bytesRead > 0)
                {
                    ms.Write(buffer, 0, bytesRead);
                }
            } while (bytesRead > 0);

            return ms.ToArray();
        }
    }
}
Sky Sanders