How do I use GZipStream with System.IO.MemoryStream?

前端 未结 9 995
遥遥无期
遥遥无期 2020-12-04 16:36

I am having an issue with this test function where I take an in memory string, compress it, and decompress it. The compression works great, but I can\'t seem to get the dec

9条回答
  •  一生所求
    2020-12-04 17:06

    I had an issue where *.CopyTo(stream)* would end up with a byte[0] result. The solution was to add .Position=0 before calling .CopyTo(stream) Answered here

    I also use a BinaryFormatter that would throw an 'End of stream encountered before parsing was completed' exception if position was not set to 0 before deserialization. Answered here

    This is the code that worked for me.

     public static byte[] SerializeAndCompressStateInformation(this IPluginWithStateInfo plugin, Dictionary stateInfo)
        {
            byte[] retArr = new byte[] { byte.MinValue };
            try
            {
                using (MemoryStream msCompressed = new MemoryStream())//what gzip writes to
                {
                    using (GZipStream gZipStream = new GZipStream(msCompressed, CompressionMode.Compress))//setting up gzip
                    using (MemoryStream msToCompress = new MemoryStream())//what the settings will serialize to
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        //serialize the info into bytes
                        formatter.Serialize(msToCompress, stateInfo);
                        //reset to 0 to read from beginning byte[0] fix.
                        msToCompress.Position = 0;
                        //this then does the compression
                        msToCompress.CopyTo(gZipStream);
                    }
                    //the compressed data as an array of bytes
                    retArr = msCompressed.ToArray();
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                throw ex;
            }
            return retArr;
        }
    
    
        public static Dictionary DeserializeAndDecompressStateInformation(this IPluginWithStateInfo plugin, byte[] stateInfo)
        {
            Dictionary settings = new Dictionary();
            try
            {
    
                using (MemoryStream msDecompressed = new MemoryStream()) //the stream that will hold the decompressed data
                {
                    using (MemoryStream msCompressed = new MemoryStream(stateInfo))//the compressed data
                    using (GZipStream gzDecomp = new GZipStream(msCompressed, CompressionMode.Decompress))//the gzip that will decompress
                    {
                        msCompressed.Position = 0;//fix for byte[0]
                        gzDecomp.CopyTo(msDecompressed);//decompress the data
                    }
                    BinaryFormatter formatter = new BinaryFormatter();
                    //prevents 'End of stream encountered' error
                    msDecompressed.Position = 0;
                    //change the decompressed data to the object
                    settings = formatter.Deserialize(msDecompressed) as Dictionary;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                throw ex;
            }
            return settings;
        }
    

提交回复
热议问题