The C1.Zip library enables a user to compress basic data types such as strings and doubles and write it into memory streams. This walkthrough takes you through the steps for compressing basic data types such as strings and doubles into streams of memory, and expand the data back when you read it from the streams.
Start a new Visual Studio project and from the Toolbox, add four Button controls along the left edge, a textbox control along the right edge of the form, and a label control towards the bottom left part of the form as shown in the snapshot in the beginning of the tutorial.
In the Properties window, make the following changes for button controls:
Button | Button.Name Property | Button.Text Property | Button.Enabled Property |
---|---|---|---|
1 | btnCompressString | Compress String | True (Default) |
2 | btnExpandString | Decompress String | False |
3 | btnCompressData | Compress Data | True (Default) |
4 | btnExpandData | Decompress Data | False |
Note that the Decompress String and Decompress Data buttons cannot be used until we have some compressed data to expand. For the Textbox control, set the MultiLine property to True, and select the ellipsis button located next to Lines property. In the String Collection Editor dialog box, type the text to use as an initial value.
For WPF applications, open the MainWindow.xaml and replace the existing XAML with the following code.
XAML |
Copy Code
|
---|---|
T<Window x:Class="CompressingandExtract_DataMemory.MainWindow" 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:CompressingandExtract_DataMemory" mc:Ignorable="d" Title="MainWindow" Height="475.1" Width="800"> <Grid> <Button x:Name="btnCompressString" Content="Compress Strings" HorizontalAlignment="Left" Height="39" Margin="10,43,0,0" VerticalAlignment="Top" Width="199" Click="BtnCompressString_Click"/> <Button x:Name="btnExpandString" Content="Expand Strings" HorizontalAlignment="Left" Height="44" Margin="10,130,0,0" VerticalAlignment="Top" Width="199" Click="BtnExpandString_Click"/> <Button x:Name="btnCompressData" Content="Compress Data" HorizontalAlignment="Left" Height="45" Margin="10,224,0,0" VerticalAlignment="Top" Width="199" Click="BtnCompressData_Click"/> <Button x:Name="btnExpandData" Content="Expand Data" HorizontalAlignment="Left" Height="44" Margin="10,323,0,0" VerticalAlignment="Top" Width="199" Click="BtnExpandData_Click"/> <RichTextBox x:Name="richTextBox1" HorizontalAlignment="Left" Height="324" Margin="267,43,0,0" VerticalAlignment="Top" Width="487"> <FlowDocument> <Paragraph> <Run Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."/> </Paragraph> </FlowDocument> </RichTextBox> <Label x:Name="label1" Content="Label" HorizontalAlignment="Left" Height="47" Margin="304,387,0,0" VerticalAlignment="Top" Width="450"/> </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:
This makes the objects defined in the C1Zip assembly visible to the project and saves a lot of typing.
Add the following code to handle the btnCompressString_Click event:
The first main line declares a member variable called m_CompressedString which will be used to hold the compressed data (encoded as a byte array). The second main line calls a utility function CompressString that compresses a given string into a byte array that can later be expanded to restore the original string. The remainder of the code is used to measure how long the compression process took and to show a dialog box with statistics. (Note that the lenBefore variable is calculated as the length of the string times two. This is because .NET strings are Unicode, and each character actually takes up two bytes.)
Add the following code which implements the CompressString function:
The main line calls the utility function ExpandString that takes a byte array and returns the original string. Add the following code for the ExpandString function:
If you run the project now, you can already experiment with string compression and decompression. You can change the text in the text box, or paste new content into it, then compress and expand the string to see how much it compresses.
Compressing binary data is just as easy as compressing strings. The only difference is that instead of attaching a StreamWriter object to the compressor stream, you attach a BinaryWriter object. Double-click the Compress Data button and add the following code to handle the btnCompressData_Click event:
The code starts by declaring a member variable called m_CompressedData which will be used to hold the compressed data (encoded as a byte array). Then it sets up the MemoryStream, C1ZStreamWriter, and BinaryWriter objects as before (the only difference is we're now using a BinaryWriter instead of a StreamWriter). Next, the code writes data into the stream using the Write method. The BinaryWriter object overloads this method so you can write all basic object types into streams. Finally, the Flush method is used as before, to make sure any cached data is written out to the compressed stream.
Expanding the compressed binary data is just a matter of setting up the decompressor stream and reading the data like you would read it from a regular stream. Add the following Click event handler code to the Decompress Data command button:
The code reads the data but does not display it. You can step through it in debug mode to make sure the data being read is the same that was written in. If you run the project and click the compress/decompress data buttons, you will see that the data is saved in an array with 14,125 bytes. To save this data in a regular stream, it would take [4 + 1000 * (4 + 8 * 3)] = 28,004 bytes. So, we compressed it to about half the original size.
This concludes the Compressing Data in Memory walkthrough.