I am using the following code for compression of data at the server:
The job of this method is to execute the specified method passed as a string and return the object as a compressed stream (serializing the object and compressing the serialized stream) to the client application. When value is returned to the client, the decompression takes place there and the decompressed stream is deserialized to obtain the actual object
The method below takes parameters as name of method to be executed including the methodarguments and argumenttypes to distinguish the different overloaded methods and identify the exact method and avoid ambiguity. ExecObject is the object with which the specified method will be invoked.
//SERVER SIDE METHOD
public Stream CompressedMethodCall(string MethodName, object[] MethodArguments, Type[] ArgumentTypes, object ExecObject)
{
object Result;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter Serializer;
System.IO.Compression.GZipStream Compressor;
Stream SerializedStream;
Stream CompressedStream;
byte[] Buffer;
try
{
MethodInfo mi = ExecObject.GetType().GetMethod(MethodName, ArgumentTypes);
Result = mi.Invoke(ExecObject, MethodArguments);
if (Result != null)
{
Serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
SerializedStream = new MemoryStream();
Serializer.Serialize(SerializedStream, Result);
SerializedStream.Position = 0;
Result = null;
CompressedStream = new MemoryStream();
Compressor = new System.IO.Compression.GZipStream(CompressedStream, System.IO.Compression.CompressionMode.Compresstrue);
while (SerializedStream.Position != SerializedStream.Length)
{
long BytesRemaining = SerializedStream.Length - SerializedStream.Position + 1;
if (BytesRemaining < BufferLength)
Buffer = new byte[BytesRemaining];
else
Buffer = new byte[BufferLength];
SerializedStream.Read(Buffer, 0, Buffer.Length);
Compressor.Write(Buffer, 0, Buffer.Length);
}
SerializedStream.Close();
CompressedStream.Position = 0;
return CompressedStream;
}
}
catch (Exception ex)
{
return null;
}
return null;
}
Now the returned value(compressed stream) is used to obtain the actual object. I am doing it this way
//CLIENT SIDE STATIC METHOD
public class DeCompressor
{
const int BufferLength = 4096;
public static object GetObject(Stream CompressedStream)
{
object Result;
BinaryFormatter DeSerializer = new BinaryFormatter();
Stream SerializedStream = new MemoryStream();
CompressedStream.Position = 0;
GZipStream DeCompressor = new GZipStream(CompressedStream, CompressionMode.Decompress,true);
byte[] Buffer;
int Read = 1;
try
{
while (Read > 0)
{
Buffer = new byte[BufferLength];
Read = DeCompressor.Read(Buffer, 0, BufferLength);
if (Read == 0) Read = DeCompressor.Read(Buffer, 0, BufferLength);
SerializedStream.Write(Buffer, 0, Read);
}
CompressedStream.Close();
SerializedStream.Position = 0;
Result = DeSerializer.Deserialize(SerializedStream);
SerializedStream.Close();
return Result;
}
catch (Exception ex)
{
Castor.CastorMessageBox.Show(ex.Message);
return null;
}
}
This method is working fine. But sometimes decompression is not producing the exact stream which was produced at server. Due to this, deserialization process is not successful and I am unable to retreive the actual object.
I am using VS2008. Any help would be appreciated