Compress Stream Information by Using DeflateStream Class in c#

Introduction

This blog contains explanation the implementation of the deflate stream class in c#. the code example shows how to compress dataset before saving it to disk and then decompress the same file before loading it back into a dataset class. The deflate stream class provides method for compressing and decompressing streams by using the deflate algorithm. 

The deflate stream class inherits directly from the stream class and ca be used in very instance where the stream class is used. For example, if you are saving a dataset as an XML file, you can use the deflate stream class to compress the xml file so that it does not consume too much space.

Note - you cannot use the deflate stream class to compress files larger than 4 gigabytes.

When you create an instance of the deflate stream class, you must pass a valid data stream and a CompressionMode enumeration value to the constructor of the deflate stream class. The CompressionMode enumeration value ca neither be CompressionMode.Compress or CompressioMode.Decompress. in this case, the constructor is overloaded so that you can pass a Boolean parameter as the third parameter. If the value of the Boolean parameter is true, the stream passes to the constructor opens, otherwise it closes.

Following code uses SaveDatasetAsCompressedXML method saves the Dataset class as compressed XML file and the REadCompressedXMLItoDataset method reads the compressed XML file into a dataset class. The Dataset class uses the WriteXml and ReadXml methods to serialize the data in the dataset to an XML file.

To compress and decompress stream make sure that you have implemented the System.IO.Compression namespace, because the DeflateStream class comes under the previous namespace.

Code Example

public static void deflatestreamdemo()

{

try

{

    FileStream FS;DeflateStream DFS;

    FileInfo FI;

    string originnalXMLFileName = "letter.xml";

    string compressesXMLFile = "letter_compressed";

    string decompresedXMLFile = "letter_decompressed";

    DataSet DS;FI = new FileInfo(originnalXMLFileName);

    Console.WriteLine("Original File Details");

    Console.WriteLine("=====-=====-=====-=====");

    Console.WriteLine("File Name :" + FI.Name);

    Console.WriteLine("Size in bytes:" + FI.Length);

    DS = new DataSet();DS.ReadXml(originnalXMLFileName);

    FS = new FileStream(compressesXMLFile, FileMode.Create, FileAccess.Write);

    DFS = new DeflateStream(FS, CompressionMode.Compress);

    FS.Close();

    DS.WriteXml(compressesXMLFile);DFS.Close();

    FI = new FileInfo(compressesXMLFile);

    Console.WriteLine("Compressed File Details");

    Console.WriteLine("=====-=====-=====-=====");

    Console.WriteLine("File Name :" + FI.Name);

    Console.WriteLine("Size in bytes:" + FI.Length);

    FS = new FileStream(compressesXMLFile, FileMode.Open, FileAccess.Read);

    DFS = new DeflateStream(FS, CompressionMode.Decompress);

    //FS.Close();DS = new DataSet();DS.ReadXml(DFS);

    DS.WriteXml(decompresedXMLFile);DFS.Close();

    FI = new FileInfo(decompresedXMLFile);

    Console.WriteLine("Compressed File Details");

    Console.WriteLine("=====-=====-=====-=====");

    Console.WriteLine("File Name :" + FI.Name);

    Console.WriteLine("Size in bytes:" + FI.Length);

    }

   catch (Exception ex)

   {

        Console.WriteLine(ex.Message);

   }

   Console.ReadLine();

}