Search code examples
c#serializationdeserializationbinaryformattergzipstream

how to deserialize GZipStream?


I am serializing a stream and it is being stored in cloud using REST services. Serialization part is given below-

public void serialize(Object obj, Stream str)
    {
        using (GZipStream zipStream = new GZipStream(str, CompressionMode.Compress, true))
        {
            if (obj is Stream)//This is executed in my case
            {
                ((Stream)obj).CopyTo(zipStream);
            }
            else
            {
                binarySerializer.serialize(obj, zipStream);
            }
        }
    }

Argument obj is the stream content and str is an empty stream which stores serialized stream for further use. If condition is executed in my case since obj is a Stream.

here is the binarySerializer.Serialize() code -

public void serialize(object obj, Stream str)
    {
        if (obj is Stream)
        {
            ((Stream)obj).CopyTo(str);
        }
        else
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(str, obj);
        }
    }

This works fine and I am able to download the serialized file(which is displayed as binary file with download option) from cloud using Restlet Client chrome extension.

Can anyone help me in writing the Deserialization part? I want the deserialization code with below signature -

public T deserialize<T>(Stream str)
    {
      //Code here
    }

Thanks in Advance!


Solution

  • If I undestand you right you can write like so

        public T deserialize<T>(Stream str) where T : class, new()
        {
            Type type = typeof(T);
            if (type == typeof(Stream))
            {
                using (var bigStr = new GZipStream(str, CompressionMode.Decompress))
                using (var outStream = new MemoryStream())
                {
                    bigStr.CopyTo(outStream);
                    return outStream as T;
                }
            }
            else
            {
                BinaryFormatter bin = new BinaryFormatter();
                return (T)bin.Deserialize(str);
            }
        }
    

    If T is a type of Stream, we are deserializing GZipStream to Stream. Other cases we are using a simple BinaryFormatter.

    After notes in the comment

    public T Deserialize<T>(Stream str)
        {
            Type type = typeof(T);
            if (type == typeof(Stream))
            {
                using (var bigStr = new GZipStream(str, CompressionMode.Decompress))
                using (var outStream = new MemoryStream())
                {
                    bigStr.CopyTo(outStream);
                    return (T)(outStream as object);
                }
            }
            else
            {
                BinaryFormatter bin = new BinaryFormatter();
                return (T)bin.Deserialize(str);
            }
        }
    

    To return result from function we convert from MemoryStream type to object type first then we can use explicit conversion from object type to T type.