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.
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.
XAML |
Copy 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> |
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.
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:
This declares the namespaces of the classes used in the project.
In the Code Editor of the form, type or copy the following lines in the body of the form implementation:
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:
Add the following code to handle the Click event for the Create Data Table button:
The function uses standard ADO.NET objects and methods to create and populate a DataTable object, which is then bound to the DataGrid control.
Add the following code to handle the Click event for the Save Data Table button:
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.
Add the following code to handle the Click event for the Load Data Table button:
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.
Add the following code to handle the Click event for the Load Compressed Data Table button:
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.