/// <Summary>
/// Serialize and compress the DataSet object
/// </Summary>
/// <Param name = "ds"> </param>
Public static void datasetserializercompression (Dataset ds, string filepath)
{
Iformatter formatter = new binaryformatter (); // defines binaryformatter to serialize the DataSet object
Memorystream MS = new memorystream (); // creates a memory stream object
Formatter. serialize (MS, DS); // serialize the DataSet object to the memory stream
Byte [] buffer = Ms. toarray (); // write the memory stream object to the byte array
Ms. Close (); // close the memory stream object
Ms. Dispose (); // release resources
Filestream FS = file. Create (filepath); // create a file
Gzipstream = new gzipstream (FS, compressionmode. Compress, true); // create a compressed object
Gzipstream. Write (buffer, 0, buffer. Length); // write the compressed data to the file.
Gzipstream. close (); // close the compressed stream. Note that you must disable it. Otherwise, files smaller than 4 K cannot read data, and files larger than 4 K cannot be read completely.
Gzipstream. Dispose (); // release an object
FS. Close (); // close the stream
FS. Dispose (); // release the object
}
/// <Summary>
/// Deserialize the compressed Dataset
/// </Summary>
/// <Param name = "_ filepath"> </param>
/// <Returns> </returns>
Public static dataset datasetdeserializedecompress (string _ filepath)
{
Filestream FS = file. openread (_ filepath); // open the file
FS. Position = 0; // set the file stream location
Gzipstream = new gzipstream (FS, compressionmode. Decompress); // create an decompressed object
Byte [] buffer = new byte [4096]; // defines the data buffer
Int offset = 0; // defines the read location
Memorystream MS = new memorystream (); // defines the memory stream
While (offset = gzipstream. Read (buffer, 0, buffer. Length ))! = 0)
{
Ms. Write (buffer, 0, offset); // The decompressed data is written to the memory stream.
}
Binaryformatter sfformatter = new binaryformatter (); // defines binaryformatter to deserialize the DataSet object
Ms. Position = 0; // set the memory stream location
Dataset Ds;
Try
{
DS = (Dataset) sfformatter. deserialize (MS); // deserialization
}
Catch
{
Throw;
}
Finally
{
Ms. Close (); // close the memory stream
Ms. Dispose (); // release resources
}
FS. Close (); // close the file stream
FS. Dispose (); // release resources
Gzipstream. Close (); // close the extract stream
Gzipstream. Dispose (); // release resources
Return Ds;
}