[]
        
(Showing Draft Content)

Serialize and Deserialize Compressed Files

Compressing .NET serializable objects like database tables to streams can reduce their size by 50-80%. The Zip library provides API to compress serializable objects into streams, and then load them back from the streams. You can easily serialize objects in compressed files, and then load them back into the memory.

Let's take an example, where a user can create a data table using the NorthWind database, such that the table will be saved (serialized) into regular and compressed streams. Further, the user will also be able to load the data back from either stream.

App UI with buttons and grid

Set up Application

  1. Start a new Visual Studio project and from the Toolbox, add the following controls to the form:

    Four Button controls along the left edge of the form, as shown in the picture above. In the Properties window make the following changes to each Button control:

    Button Button.Text Property Button.Name Property Button.Enabled Property
    1 Create Data Table btnCreate True (Default)
    2 Save Data Table btnSave False
    3 Load Data Table btnLoad False
    4 Load Compressed Data Table btnLoadCompressed False

    A DataGridView control on the right of the form. A ToolStripStatusLabel control docked at the bottom of the form. To add this control, first add a StatusStrip control to the form. Then click the Add ToolStripStatusLabel drop-down arrow and select StatusLabel. A ToolStripStatusLabel control appears and is docked at the bottom of the form.

    For WPF applications, open the MainWindow.xaml and replace the existing XAML with the following code.

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Serialization"
            xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml" x:Class="Serialization.MainWindow"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <Button x:Name="btnCreate" Content="Create Data Table" HorizontalAlignment="Left" Margin="39,92,0,0" VerticalAlignment="Top" Width="164" Height="32" Click="BtnCreate_Click"/>
            <Button x:Name="btnSave" Content="Save Data Table" HorizontalAlignment="Left" Margin="39,159,0,0" VerticalAlignment="Top" Width="164" Height="33" Click="BtnSave_Click"/>
            <Button x:Name="btnLoad" Content="Load Data Table" HorizontalAlignment="Left" Margin="39,231,0,0" VerticalAlignment="Top" Width="164" Height="33" Click="BtnLoad_Click"/>
            <Button x:Name="btnLoadCompressed" Content="Load Compressed Data Table" HorizontalAlignment="Left" Margin="39,301,0,0" VerticalAlignment="Top" Width="164" Height="38" Click="BtnLoadCompressed_Click"/>
            <DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Height="229" Margin="242,92,0,0" VerticalAlignment="Top" Width="500"/>        
            <Label x:Name="label1" Content="" HorizontalAlignment="Left" Height="33" Margin="242,376,0,0" VerticalAlignment="Top" Width="500"/>
        </Grid>
    </Window>
    

Add Reference Assemblies

  1. Go to the Solution Explorer window and click the Show All Files button. Right-click on References, and select the Add Reference menu option. Select the C1.Zip assembly from the list.

  2. Select the Form1.vb tab (Form1.cs in C#) or go to View|Code to open the Code Editor. At the top of the file, add the following statements:

    WinForms

    This is the C# Code for WinForms applications:

    using System.IO;
    using C1.Zip;
    

    This is the VB Code for WinForms applications:

    Imports C1.Zip
    Imports System.IO
    

    WPF

    This is the C# code for WPF applications:

    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using C1.Zip;
    

    This is the VB code for WPF applications:

    Imports System.IO
    Imports System.Runtime.Serialization.Formatters.Binary
    Imports C1.Zip
    

    This declares the namespaces of the classes used in the project.

Declare constants

  1. In the Code Editor of the form, type or copy the following lines in the body of the form implementation:

    WinForms

    This is the C# Code for WinForms applications:

    private const string FN_REGULAR = @"\DataTable.regular";
    private const string FN_COMPRESSED = @"\DataTable.compressed";
    

    This is the VB Code for WinForms applications:

    Private Const FN_REGULAR As String = "\DataTable.regular"
    Private Const FN_COMPRESSED As String = "\DataTable.compressed"
    

    WPF

    This is the C# code for WPF applications:

    private const string FN_REGULAR = @"\regular";
    private const string FN_COMPRESSED = @"\compressed";
    PersonList personList = new PersonList();
    

    This is the VB code for WPF applications:

    Private Const FN_REGULAR As String = "\regular"
    Private Const FN_COMPRESSED As String = "\compressed"
    Private f_PersonList As PersonList = New PersonList()
    

    These constants define the name of the database used to populate the data table and the file names used to serialize the data.

    For WPF applications, add the following code below the namespace declaration:

    csharp

    [Serializable]
    public class Person
    {
        public string FirstName
        {
            get;
            set;
        }
        public string LastName
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
        public string City
        {
            get;
            set;
        }
    }
    [Serializable]
    public class PersonList
    {
        private List<Person> _persons = new List<Person>();
        public List<Person> Persons
        {
            get
            {
                return this._persons;
            }
            set
            {
                this._persons = value;
            }
        }
    }
    

    vbnet

    <Serializable>
    Public Class Person
        Public Property FirstName As String
        Public Property LastName As String
        Public Property Age As Integer
        Public Property City As String
    End Class
    <Serializable>
    Public Class PersonList
        Private _persons As List(Of Person) = New List(Of Person)()
        Public Property Persons As List(Of Person)
            Get
                Return _persons
            End Get
            Set(ByVal value As List(Of Person))
                _persons = value
            End Set
        End Property
    End Class
    

Create DataTable

  1. Add the following code to handle the Click event for the Create Data Table button:

    WinForms

    This is the C# Code for WinForms applications:

    // create DataTable object and populate it from NorthWind database
    void btnCreate_Click(object sender, System.EventArgs e)
    {
        // open table
        string conn = GetConnectionString();
        string rs = "select * from customers";            
        // show status
        Cursor = Cursors.WaitCursor;
        statusBar1.Text = "Loading data from mdb file...";
        // load data
        OleDbDataAdapter da = new OleDbDataAdapter(rs, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        // show status
        Cursor = Cursors.Default;
        statusBar1.Text = "Loaded " + ds.Tables[0].Rows.Count + " records from mdb file.";
        // bind to grid
        dataGrid1.DataSource = ds.Tables[0];
        // enable save button
        btnSave.Enabled = true;
    }
    

    This is the VB Code for WinForms applications:

    ' create DataTable object and populate it from NorthWind database
    Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        ' open table
        Dim conn As String = GetConnectionString()
        Dim rs As String = "select * from customers"
        ' show status
        Cursor = Cursors.WaitCursor
        statusBar1.Text = "Loading data from mdb file..."
        ' load data
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(rs, conn)
        Dim ds As DataSet = New DataSet()
        da.Fill(ds)
        ' show status
        Cursor = Cursors.Default
        statusBar1.Text = "Loaded " & ds.Tables(0).Rows.Count & " records from mdb file."
        ' bind to grid
        dataGrid1.DataSource = ds.Tables(0)
        ' enable save button
        btnSave.Enabled = True
    End Sub
    

    WPF

    This is the C# code for WPF applications:

    private void BtnCreate_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 1000; i++)
        {
            personList.Persons.Add(new Person()
            {
                FirstName = string.Format("First Name {0}", i),
                LastName = string.Format("Last Name {0}", i),
                Age = i,
                City = string.Format("City {0}", i)
            });
        }
        this.dataGrid1.ItemsSource = personList.Persons;
    }
    

    This is the VB code for WPF applications:

    Private Sub BtnCreate_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        For i As Integer = 0 To 1000 - 1
            f_PersonList.Persons.Add(New Person() With {
                    .FirstName = String.Format("First Name {0}", i),
                    .LastName = String.Format("Last Name {0}", i),
                    .Age = i,
                    .City = String.Format("City {0}", i)
                })
        Next
        Me.dataGrid1.ItemsSource = f_PersonList.Persons
    End Sub
    

    The function uses standard ADO.NET objects and methods to create and populate a DataTable object, which is then bound to the DataGrid control.

Save DataTable

  1. Add the following code to handle the Click event for the Save Data Table button:

    WinForms

    This is the C# Code for WinForms applications:

    // save DataTable object into two files: regular and compressed versions.
    void btnSave_Click(object sender, System.EventArgs e)
    {
        // get data table from grid
        DataTable dt = dataGrid1.DataSource as DataTable;
        Debug.Assert(dt != null);
        // show status
        Cursor = Cursors.WaitCursor;
        statusBar1.Text = "Serializing data to regular file...";
        // serialize the data set to a regular file
        string fn = Application.StartupPath + FN_REGULAR;
        FileStream fs = new FileStream(fn, FileMode.Create);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fs, dt);
        long lenRegular = fs.Length;
        fs.Close();
        // show status
        Cursor = Cursors.WaitCursor;
        statusBar1.Text = "Serializing data to compressed file...";
        // serialize the data set to a compressed file
        fn = Application.StartupPath + FN_COMPRESSED;
        fs = new FileStream(fn, FileMode.Create);
        C1ZStreamWriter compressor = new C1ZStreamWriter(fs);            
        bf = new BinaryFormatter();
        bf.Serialize(compressor, dt);
        long lenCompressed = fs.Length;
        fs.Close();
        // show status
        Cursor = Cursors.Default;
        statusBar1.Text = string.Format(
            "Saved to regular file ({0:#,###} bytes) and " +
            "compressed file ({1:#,###} bytes)",
            lenRegular, lenCompressed);
        // enable load buttons
        btnLoad.Enabled = true;
        btnLoadCompressed.Enabled = true;
    }
    

    This is the VB Code for WinForms applications:

    ' save DataTable object into two files: regular and compressed versions.
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        ' get data table from grid
        Dim dt As DataTable = dataGrid1.DataSource
        ' show status
        Cursor = Cursors.WaitCursor
        statusBar1.Text = "Serializing data to regular file..."
        ' serialize the data set to a regular file
        Dim fn As String = Application.StartupPath + FN_REGULAR
        Dim fs As FileStream = New FileStream(fn, FileMode.Create)
        Dim bf As BinaryFormatter = New BinaryFormatter()
        bf.Serialize(fs, dt)
        Dim lenRegular As Long = fs.Length
        fs.Close()
        ' show status
        Cursor = Cursors.WaitCursor
        statusBar1.Text = "Serializing data to compressed file..."
        ' serialize the data set to a compressed file
        fn = Application.StartupPath & FN_COMPRESSED
        fs = New FileStream(fn, FileMode.Create)
        Dim compressor As C1ZStreamWriter = New C1ZStreamWriter(fs)
        bf = New BinaryFormatter()
        bf.Serialize(compressor, dt)
        Dim lenCompressed As Long = fs.Length
        fs.Close()
        ' show status
        Cursor = Cursors.Default
        statusBar1.Text = String.Format(
                "Saved to regular file ({0:#,###} bytes) and " &
                "compressed file ({1:#,###} bytes)",
                lenRegular, lenCompressed)
        ' enable load buttons
        btnLoad.Enabled = True
        btnLoadCompressed.Enabled = True
    End Sub
    

    WPF

    This is the C# code for WPF applications:

    private void BtnSave_Click(object sender, RoutedEventArgs e)
    {
        // Show status.   
        Cursor = Cursors.Wait;
        label1.Content = "Serializing data to regular file...";
        // Serialize the data set to a regular file.
        string fn = Environment.CurrentDirectory + FN_REGULAR;
        FileStream fs = new FileStream(fn, FileMode.Create);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fs, personList);
        long lenRegular = fs.Length;
        fs.Close();
        // Show status.   
        Cursor = Cursors.Wait;
        label1.Content = "Serializing data to compressed file...";
        // Serialize the data set to a compressed file.  
        fn = Environment.CurrentDirectory + FN_COMPRESSED;
        fs = new FileStream(fn, FileMode.Create);
        C1ZStreamWriter compressor = new C1ZStreamWriter(fs);
        bf = new BinaryFormatter();
        bf.Serialize(compressor, personList);
        long lenCompressed = fs.Length;
        fs.Close();
        // Show status.   
        Cursor = Cursors.Hand;
        label1.Content = string.Format("Saved to regular file ({0:#,###} bytes) and " + "compressed file ({1:#,###} bytes)", lenRegular, lenCompressed);
        // Enable the load buttons.   
        btnLoad.IsEnabled = true;
        btnLoadCompressed.IsEnabled = true;
    }
    

    This is the VB code for WPF applications:

    Private Sub BtnSave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Show status.   
        Cursor = Cursors.Wait
        Me.label1.Content = "Serializing data to regular file..."
        ' Serialize the data set to a regular file.
        Dim fn As String = Environment.CurrentDirectory & FN_REGULAR
        Dim fs As FileStream = New FileStream(fn, FileMode.Create)
        Dim bf As BinaryFormatter = New BinaryFormatter()
        bf.Serialize(fs, f_PersonList)
        Dim lenRegular As Long = fs.Length
        fs.Close()
        ' Show status.   
        Cursor = Cursors.Wait
        Me.label1.Content = "Serializing data to compressed file..."
        ' Serialize the data set to a compressed file.  
        fn = Environment.CurrentDirectory & FN_COMPRESSED
        fs = New FileStream(fn, FileMode.Create)
        Dim compressor As C1ZStreamWriter = New C1ZStreamWriter(fs)
        bf = New BinaryFormatter()
        bf.Serialize(compressor, f_PersonList)
        Dim lenCompressed As Long = fs.Length
        fs.Close()
        ' Show status.   
        Cursor = Cursors.Hand
        Me.label1.Content = String.Format("Saved to regular file ({0:#,###} bytes) and " & "compressed file ({1:#,###} bytes)", lenRegular, lenCompressed)
        ' Enable the load buttons.   
        Me.btnLoad.IsEnabled = True
        Me.btnLoadCompressed.IsEnabled = True
    End Sub
    

    The first set of code serializes the DataTable into a regular file, and the second serializes the DataTable into a compressed file. Note that only one additional line is required to compress the data.

    In both cases, the serialization is executed by the BinaryFormatter object. The only difference is that in the first case, the Serialize method is called with a regular file stream as a parameter; in the second, a C1ZStreamWriter is used instead.

Load DataTable from Regular File

  1. Add the following code to handle the Click event for the Load Data Table button:

    WinForms

    This is the C# Code for WinForms applications:

    void btnLoad_Click(object sender, System.EventArgs e)
    {
        // clear grid, show status
        Cursor = Cursors.WaitCursor;
        dataGrid1.DataSource = null;
        statusBar1.Text = "Loading DataTable from regular file...";
        // deserialize from regular file
        string fn = Application.StartupPath + FN_REGULAR;
        FileStream fs = new FileStream(fn, FileMode.Open);
        BinaryFormatter bf = new BinaryFormatter();
        long ticks = DateTime.Now.Ticks;
        DataTable dt = (DataTable)bf.Deserialize(fs);
        long ms = (DateTime.Now.Ticks - ticks)/TimeSpan.TicksPerMillisecond;
        fs.Close();
        // show result
        Cursor = Cursors.Default;
        dataGrid1.DataSource = dt;
        statusBar1.Text = "Loaded data from regular file in " + ms.ToString() + " ms.";
    }
    

    This is the VB Code for WinForms applications:

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        ' clear grid, show status
        Cursor = Cursors.WaitCursor
        dataGrid1.DataSource = Nothing
        statusBar1.Text = "Loading DataTable from regular file..."
        ' deserialize from regular file
        Dim fn As String = Application.StartupPath & FN_REGULAR
        Dim fs As FileStream = New FileStream(fn, FileMode.Open)
        Dim bf As BinaryFormatter = New BinaryFormatter()
        Dim ticks As Long = DateTime.Now.Ticks
        Dim dt As DataTable = bf.Deserialize(fs)
        Dim ms As Long = (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond
        fs.Close()
        ' show result
        Cursor = Cursors.Default
        dataGrid1.DataSource = dt
        statusBar1.Text = "Loaded data from regular file in " & ms.ToString() & " ms."
    End Sub
    

    WPF

    This is the C# code for WPF applications:

    private void BtnLoad_Click(object sender, RoutedEventArgs e)
    {
        // Clear grid, show status.   
        Cursor = Cursors.Wait;
        dataGrid1.ItemsSource = null;
        label1.Content = "Loading from regular file...";
        // Deserialize from regular file.   
        string fn = Environment.CurrentDirectory + FN_REGULAR;
        FileStream fs = new FileStream(fn, FileMode.Open);
        long ticks = DateTime.Now.Ticks;
        BinaryFormatter bf = new BinaryFormatter();
        PersonList pl = (PersonList)bf.Deserialize(fs);
        long ms = (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond;
        fs.Close();
        // Show result.   
        Cursor = Cursors.Hand;
        dataGrid1.ItemsSource = pl.Persons;
        label1.Content = "Loaded from regular file in " + ms.ToString() + " ms.";
    }
    

    This is the VB code for WPF applications:

    Private Sub BtnLoad_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Clear grid, show status.   
        Cursor = Cursors.Wait
        Me.dataGrid1.ItemsSource = Nothing
        Me.label1.Content = "Loading from regular file..."
        ' Deserialize from regular file.   
        Dim fn As String = Environment.CurrentDirectory & FN_REGULAR
        Dim fs As FileStream = New FileStream(fn, FileMode.Open)
        Dim ticks As Long = Date.Now.Ticks
        Dim bf As BinaryFormatter = New BinaryFormatter()
        Dim pl As PersonList = CType(bf.Deserialize(fs), PersonList)
        Dim ms As Long = (Date.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond
        fs.Close()
        ' Show result.   
        Cursor = Cursors.Hand
        Me.dataGrid1.ItemsSource = pl.Persons
        Me.label1.Content = "Loaded from regular file in " & ms.ToString() & " ms."
    End Sub
    

    The first main line of code creates a new BinaryFormatter object and the second one calls its Deserialize method. The Deserialize method takes a single parameter: the stream in which the object is defined. In this case, the stream is a regular file stream.

Load DataTable from Compressed File

  1. Add the following code to handle the Click event for the Load Compressed Data Table button:

    WinForms

    This is the C# Code for WinForms applications:

    void btnLoadCompressed_Click(object sender, System.EventArgs e)
    {
        // clear grid, show status
        Cursor = Cursors.WaitCursor;
        dataGrid1.DataSource = null;
        statusBar1.Text = "Loading DataTable from compressed file...";
        // deserialize from regular file
        string fn = Application.StartupPath + FN_COMPRESSED;
        FileStream fs = new FileStream(fn, FileMode.Open);
        C1ZStreamReader decompressor = new C1ZStreamReader(fs);
        BinaryFormatter bf = new BinaryFormatter();
        long ticks = DateTime.Now.Ticks;
        DataTable dt = (DataTable)bf.Deserialize(decompressor);
        long ms = (DateTime.Now.Ticks - ticks)/TimeSpan.TicksPerMillisecond;
        fs.Close();
        // show result
        Cursor = Cursors.Default;
        dataGrid1.DataSource = dt;
        statusBar1.Text = "Loaded data from compressed file in " + ms.ToString() + " ms.";
    }
    

    This is the VB Code for WinForms applications:

    Private Sub btnLoadCompressed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadCompressed.Click
        ' clear grid, show status
        Cursor = Cursors.WaitCursor
        dataGrid1.DataSource = Nothing
        statusBar1.Text = "Loading DataTable from compressed file..."
        ' deserialize from compressed file
        Dim fn As String = Application.StartupPath + FN_COMPRESSED
        Dim fs As FileStream = New FileStream(fn, FileMode.Open)
        Dim decompressor As C1ZStreamReader = New C1ZStreamReader(fs)
        Dim bf As BinaryFormatter = New BinaryFormatter()
        Dim ticks As Long = DateTime.Now.Ticks
        Dim dt As DataTable = bf.Deserialize(decompressor)
        Dim ms As Long = (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond
        fs.Close()
        ' show result
        Cursor = Cursors.Default
        dataGrid1.DataSource = dt
        statusBar1.Text = "Loaded data from compressed file in " & ms.ToString() & " ms."
    End Sub
    

    WPF

    This is the C# code for WPF applications:

    private void BtnLoadCompressed_Click(object sender, RoutedEventArgs e)
    {
        // Clear grid, show status.   
        Cursor = Cursors.Wait;
        dataGrid1.ItemsSource = null;
        label1.Content = "Loading from compressed file...";
        // Deserialize from compressed file.   
        string fn = Environment.CurrentDirectory + FN_COMPRESSED;
        FileStream fs = new FileStream(fn, FileMode.Open);
        long ticks = DateTime.Now.Ticks;
        C1ZStreamReader decompressor;
        decompressor = new C1ZStreamReader(fs);
        BinaryFormatter bf = new BinaryFormatter();
        PersonList pl = (PersonList)bf.Deserialize(decompressor);
        long ms = (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond; fs.Close();
        // Show result.   
        Cursor = Cursors.Hand;
        dataGrid1.ItemsSource = pl.Persons;
        label1.Content = "Loaded from compressed file in " + ms.ToString() + " ms.";
    }
    

    This is the VB code for WPF applications:

    Private Sub BtnLoadCompressed_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Clear grid, show status.   
            Cursor = Cursors.Wait
        Me.dataGrid1.ItemsSource = Nothing
        Me.label1.Content = "Loading from compressed file..."
        ' Deserialize from compressed file.   
            Dim fn As String = Environment.CurrentDirectory & FN_COMPRESSED
        Dim fs As FileStream = New FileStream(fn, FileMode.Open)
        Dim ticks As Long = Date.Now.Ticks
        Dim decompressor As C1ZStreamReader
        decompressor = New C1ZStreamReader(fs)
        Dim bf As BinaryFormatter = New BinaryFormatter()
        Dim pl As PersonList = CType(bf.Deserialize(decompressor), PersonList)
        Dim ms As Long = (Date.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond
        fs.Close()
        ' Show result.   
            Cursor = Cursors.Hand
        Me.dataGrid1.ItemsSource = pl.Persons
        Me.label1.Content = "Loaded from compressed file in " & ms.ToString() & " ms."
    End Sub
    End Class
    

    The main lines are similar to the code used to deserialize data from a regular file. The only difference is that instead of passing a regular file stream to the Deserialize method, we now use a C1ZStreamReader object.

See Also

Password Protecting Zip Files