DataSet object and compress

Source: Internet
Author: User

/// <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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.