C1Zip allows the user to compress individual files into compressed .NET streams (not zip files) on the disk. This is highly useful when the user wants to compress all files in an application directory.
The Zip library provides medium-level classes like C1ZStreamWriter class and C1ZStreamReader class to use data compression on any .NET streams, not only in zip files.
Medium-level Classes | Description |
C1ZStreamWriter class |
It compresses data into .NET Streams. This can be done by creating a C1ZStreamWriter object passing the stream to the C1ZStreamWriter constructor. |
C1ZStreamReader class |
It decompresses data from .NET streams. This can be done by creating a C1ZStreamReader object and passing the compressed stream to the C1ZStreamReader constructor. |
The flow-diagram below illustrates how these classes work:
Now that you got an idea about the medium-level classes in C1Zip library, let's see how we can compress and decompress files on .NET streams using the classes.
Start a new Visual Studio project and from the Toolbox, add two Button controls along the left edge and label control along the right edge of the form, as shown in the snapshot in the beginning of the tutorial.
In the Properties window make the following changes:
Button | Button.Text Property | Button.Name Property | Button.Enabled Property |
---|---|---|---|
1 | Compress Files | btnCompress | True (Default) |
2 | Expand Files | btnExpand | False |
Note that the Expand Files button cannot be used until we have some compressed files to expand. The Label control will display statistics about the compression/expanding process.
For WPF applications, open the MainWindow.xaml and replace the existing XAML with the following code.
XAML |
Copy Code
|
---|---|
<Window x:Class="FilestoStreams_WPFCSharp.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:FilestoStreams_WPFCSharp" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid HorizontalAlignment="Left" Width="792"> <Button x:Name="btnCompress" Content="Compress Files" HorizontalAlignment="Left" Height="49" Margin="50,60,0,0" VerticalAlignment="Top" Width="219" Click="BtnCompress_Click"/> <Button x:Name="btnExpand" Content="Expand Files" HorizontalAlignment="Left" Height="48" Margin="50,271,0,0" VerticalAlignment="Top" Width="219" IsEnabled="False" Click="BtnExpand_Click"/> <Label x:Name="label1" Content="" HorizontalAlignment="Left" Height="259" Margin="328,60,0,0" VerticalAlignment="Top" Width="395"/> </Grid> </Window> |
Add a reference to the C1.Zip assembly. Define the directory names for the compressed and expanded files.
Add the following code to handle the Click event for the Compress Files button:
Add the code for CompressFile method. The main line calls the utility function CompressFile method to compress each selected file. The compressed files are stored in the \compressed directory under the application folder. They have the same name as the original file, plus a CMP extension. The function starts by creating two new file streams: one for the source file and the other for the compressed file. Then it creates a C1ZStreamWriter object and attaches it to the destination stream.
Add the code for StreamCopy function. It transfers data from the source file and write it into the compressor stream. To put it simply, the StreamCopy function simply copies bytes from one stream to another.
Note that the function calls the Flush method after it is done to ensure that any cached data is written out when the function is done copying. This is especially important when dealing with compressed streams, since they cache substantial amounts of data in order to achieve good compression rates.
Add the following code to handle the Click event for the Expand Files button:
Add the ExpandFile method. The main line calls the utility function ExpandFile utility method to expand the files that were compressed earlier. The expanded files are stored in the \expanded directory under the application folder. They have the same name as the original file, minus the CMP extension. This function is similar to CompressFile, except that it attaches a C1ZStreamReader to the source stream instead of attaching a C1ZStreamWriter to the destination stream.
Now, that you have you completed all the steps, press F5 to run the application.