Datasets or collection of data like database tables can be compressed with the Zip library so that users can conveniently save datasets into single file, without consuming up much disk space.
This walkthrough takes you through the steps for compressing datasets to zip files.
Start a new Visual Studio project and from the Toolbox, add three Button controls along the left edge, a DataGrid control along the right edge of the form, and aStatusBar control towards the bottom of the form as shown in the snapshot in the beginning of the walkthrough.
In the Properties window, make the following changes for button controls:
Button | Button.Name Property | Button.Text Property | Button.Enabled Property |
---|---|---|---|
1 | btnCreate | Create Data Table | True (Default) |
2 | btnSave | Write Datasets | False |
3 | btnLoad | Read Datasets | False |
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="457.49" Width="916.096"> <Grid Margin="0,0,-92,-150"> <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="Write DataSets" HorizontalAlignment="Left" Margin="39,159,0,0" VerticalAlignment="Top" Width="164" Height="33" Click="BtnSave_Click"/> <Button x:Name="BtnLoad" Content="Read DataSets" HorizontalAlignment="Left" Margin="39,231,0,0" VerticalAlignment="Top" Width="164" Height="33" Click="BtnLoad_Click"/> <DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Height="229" Margin="242,92,0,0" VerticalAlignment="Top" Width="500"/> <StatusBar x:Name="statusBar1" HorizontalAlignment="Left" Height="22" Margin="20,464,-916,-486" VerticalAlignment="Top" Width="896"/> <Label x:Name="label1" Content="Label" HorizontalAlignment="Left" Height="33" Margin="242,376,0,0" VerticalAlignment="Top" Width="500" Visibility="Collapsed"/> </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 to add the C1.Zip assembly from the list.
Open the Code Editor. and add the following statements at the top of the file:
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 code given below for GetConnectionString()
Add the following code to handle the Click event for the Write Datasets button:
From the code snippet, it is clear that a C1ZipFile object is initialized and a zip file is opened at the temporary location, where you want to save the dataset using the Open method of C1ZipFile class. The OpenWriter method of C1ZipEntryCollection classis invoked to write the dataset into the zip file. This method opens a stream for writing this entry.
Add the following code to handle the Click event for the Read Datasets button:
Here, after opening the zip file using the Open method of C1ZipFile class, the OpenReader method of C1ZipEntry class is invoked to read the dataset into the zip file, without extracting it to the zip file.
This concludes the walkthrough for compressing datasets.