|I am successfully using .NET's BinaryFormatter to serialize a very complex object (an instance, at run-time) into a MemoryStream, and I can save that to a file, and then de-serialize the file back into an instance of the object.
Then, I wanted to add compressing the MemoryStream using GZip , before saving it to a file.
I am, without compile- or run- time error, using .NET's GZip facility to GZip the created MemoryStream, and then save it to a disk file.
To summarize the save-to-file sequence:
1. use BinaryFormatter to serialize the object to a MemoryStream
2. use GZip to compress the MemoryStream
3. write the Gzipped MemoryStream to disk
I've already learned the hard way that there is a quirk in using GZip in .NET in compressing [^].
I verify that the file is written, and it appears to have been "shrunk" by the GZip facility.
But, when I try to reverse the process, to essentially deserialize (recreate) the object by:
1. reading the Gzipped saved file into a MemoryStream
2. using GZip's Decompress on the MemoryStream
3. using BinaryFormatter to deserialize the MemoryStream, which I then cast into the Type of the original object: fail.
The point I am stuck at is expressed in this code:
using (GZipStream deCompressionStream = new GZipStream(new MemoryStream(), CompressionMode.Decompress))
BinaryFormatter bFormatter = new BinaryFormatter();
object obj = bFormatter.Deserialize(newMemoryStream);
Thanks in advance for any advice.
 I set out to explore using GZip as a way to increase my general knowledge of .NET's stream facilities, and after learning how to successfully use Mehdi Gholam's fastBinaryJSON work here on CP, and using the MiniLZO class in Mehdi's Raptor Document Store.
“Human beings do not live in the objective world alone, nor alone in the world of social activity as ordinarily understood, but are very much at the mercy of the particular language which has become the medium of expression for their society. It is quite an illusion to imagine that one adjusts to reality essentially without the use of language and that language is merely an incidental means of solving specific problems of communication or reflection." Edward Sapir, 1929