ZipInputStream help.

Mar 15, 2012 at 2:24 PM


  Just starting to use DotNetZip library and hoping I am just overlooking something obvious.  I am compressing text on my server, then sending it to a client for decompression.  I don't want to use files, so I've been using memory streams.  The problem I am having is that when I try do get the data using ZipInputStream it does not fill the buffer.  I can see the compressed string is getting to the client, but can't uncompress it properly.


Here is the compression code:

        public string Compress(string text)
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            MemoryStream ms = new MemoryStream();

            using (ZipOutputStream zip = new ZipOutputStream(ms))
                zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
                zip.Write(buffer, 0, buffer.Length);

            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];
            System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
            System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);

            return Convert.ToBase64String(gzBuffer);

And, the decompression code:

    public string Decompress(string compressedText)

        byte[] compressedBuffer = Convert.FromBase64String(compressedText);

        using (MemoryStream ms = new MemoryStream())
            int msgLength = BitConverter.ToInt32(compressedBuffer, 0);
            ms.Write(compressedBuffer, 4, compressedBuffer.Length - 4);

            byte[] buffer = new byte[msgLength];

            ms.Position = 0;
            using (ZipInputStream zip = new ZipInputStream(ms))
                zip.Read(buffer, 0, buffer.Length);

            return Encoding.UTF8.GetString(buffer);

What I'm seeing is when I get to the ZipInputStream portion, the memory stream (ms) has data, but after the zip.Read() the buffer is still empty.  Anyone see what I am doing wrong?