Yesterday we talked about dataset serialization and compression. Today we extract and deserializeCodeWrite down:
/// <Summary>
/// Deserialization and compression of Dataset
/// </Summary>
/// <Param name = "_ filepath"> </param>
/// <Returns> </returns>
Static Dataset datasetdeserializedecompress ( String _ Filepath)
{
Filestream FS = File. openread (_ filepath ); // Open a file
FS. Position = 0 ; // Set the file stream location
Gzipstream = New Gzipstream (FS, compressionmode. Decompress ); // Create extract object
Byte [] Buffer = New Byte [ 4096 ]; // Define data buffer
Int Offset = 0 ; // Define the read location
Memorystream MS = New Memorystream (); // Define memory stream
While (Offset = Gzipstream. Read (buffer, 0 , Buffer. Length )) ! = 0 )
{
Ms. Write (buffer, 0 , Offset ); // Decompressed data is written to the memory stream
}
Binaryformatter sfformatter= NewBinaryformatter ();//Define 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 (); // Disable memory stream
Ms. Dispose (); // Release resources
}
FS. Close (); // Close file stream
FS. Dispose (); // Release resources
Gzipstream. Close (); // Close extract stream
Gzipstream. Dispose (); // Release resources
Return DS;
}
/// <Summary>
/// Deserialization of uncompressed Dataset
/// </Summary>
/// <Param name = "_ filepath"> </param>
/// <Returns> </returns>
Static Dataset datasetdeserialize ( String _ Filepath)
{
Filestream FS = File. openread (_ filepath ); // Open a file
FS. Position = 0 ; // Set the file stream location
Binaryformatter sfformatter = New Binaryformatter (); // Define binaryformatter to deserialize the DataSet object
Dataset Ds;
Try
{
DS = (Dataset) sfformatter. deserialize (FS ); // Deserialization
}
Catch
{
Throw ;
}
Finally
{
FS. Close (); // Disable memory stream
FS. Dispose (); // Release resources
}
FS. Close (); // Close file stream
FS. Dispose (); // Release resources
Return DS;
}