The C1.Zip service library provides high-level classes like C1ZipFile and C1ZipEntry classes to support lossless data compression. In other words, the user can perfectly reconstruct the original data from the compressed data.
High-level Classes | Description |
C1ZipFile Class | It encapsulates a zip file. After you create a C1ZipFile object, you can attach it to an existing zip file or tell it to create a new empty zip file for you. |
C1ZipEntryCollection Class | It can be used after creating or opening a zip file. So, basically it's used after calling the C1ZipFile class. The C1ZipEntryCollection class uses the Entries collection to inspect the contents of the zip file, or to add, extract/decompress, and delete/remove entries. |
C1ZipEntry Class | It exposes properties and methods that describe each entry, including its original file name, size, compressed size, checksum and so on. |
In this topic, we will cover all the basic operations that can be done using the high-level classes in a zip file.
Here, we are following the same steps as in Quickstart, such as adding Windows controls to the form, adding reference assemblies, and declaring and initializing C1ZipFile object. But, please note the changes to be made in the Properties Window for the 7 Button controls:
Button | Button.Name Property | Button.Text Property |
1 | btnNew | Create Zip File |
2 | btnAdd | Add Files |
3 | btnOpen | Open Files |
4 | btnExtract | Extract Files |
5 | btnRemove | Remove Files |
6 | btnView | View Files |
7 | btnTest | Test Zip Files |
For WPF applications, open the MainWindow.xaml and replace the existing XAML with the following code.
XAML |
Copy Code
|
---|---|
<Window x:Class="CompressandExpandFiles.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:CompressandExpandFiles" mc:Ignorable="d" WindowState="Maximized" Title="MainWindow" Height="450" Width="800"> <Grid x:Name="LayoutRoot" > <Grid x:Name="_mainpage" Margin="0,0,0,-31" Height="450" VerticalAlignment="Top" Loaded="Grid_Loaded"> <Grid.ColumnDefinitions> <ColumnDefinition Width="439*"/> <ColumnDefinition Width="23*"/> <ColumnDefinition Width="330*"/> </Grid.ColumnDefinitions> <Button x:Name="btnNew" Content="Create Zip File…" HorizontalAlignment="Left" Margin="74,10,0,0" VerticalAlignment="Top" Width="107" RenderTransformOrigin="0.346,-2.804" Click="BtnNew_Click" Height="31"/> <Button x:Name="btnOpen" Content="Open Zip File…" HorizontalAlignment="Left" Margin="74,70,0,0" VerticalAlignment="Top" Width="107" Click="BtnOpen_Click" Height="33"/> <Button x:Name="btnAdd" Content="Add Files…" HorizontalAlignment="Left" Margin="74,141,0,0" VerticalAlignment="Top" Width="107" RenderTransformOrigin="0.37,-2.527" Click="BtnAdd_Click" Height="25"/> <Button x:Name="btnExtract" Content="Extract Files" HorizontalAlignment="Left" Height="28" Margin="74,200,0,0" VerticalAlignment="Top" Width="107" Click="BtnExtract_Click"/> <Button x:Name="btnRemove" Content="Remove Files" HorizontalAlignment="Left" Height="26" Margin="74,260,0,0" VerticalAlignment="Top" Width="107" Click="BtnRemove_Click"/> <Button x:Name="btnView" Content="View File" HorizontalAlignment="Left" Margin="74,314,0,0" VerticalAlignment="Top" Width="107" RenderTransformOrigin="0.323,-1.641" Click="BtnView_Click" Height="26"/> <Button x:Name="btnTest" Content="Test Zip File" HorizontalAlignment="Left" Margin="74,378,0,0" VerticalAlignment="Top" Width="107" Click="BtnTest_Click" Height="24"/> <ListView x:Name="listView1" HorizontalAlignment="Left" Height="315" Margin="240,49,0,0" VerticalAlignment="Top" Width="514" Grid.ColumnSpan="3"> <ListView.View> <GridView x:Name="Grid"> <GridViewColumn DisplayMemberBinding="{Binding FileName}" Header="File" /> <GridViewColumn DisplayMemberBinding="{Binding Date}" HeaderStringFormat="MM/dd/yy" Header="Date" /> <GridViewColumn DisplayMemberBinding="{Binding SizeUncompressed}" HeaderStringFormat="#,##0" Header="Size"/> <GridViewColumn DisplayMemberBinding="{Binding SizeCompressed}" HeaderStringFormat="#,##0" Header="Compressed"/> <GridViewColumn DisplayMemberBinding="{Binding pct}" HeaderStringFormat="00 %" Header="Amount"/> </GridView> </ListView.View> </ListView> </Grid> <Grid x:Name="_preview" Grid.RowSpan="4" Visibility="Collapsed" d:IsHidden="True" > <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition /> </Grid.RowDefinitions> <Grid Margin="0 0 0 20"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text="C1Zip Preview" FontSize="28" Margin="0 0 40 0"/> <Button Grid.Column="1" x:Name="_btnClosePreview" Content="Close" Click="_btnClosePreview_Click_1" HorizontalAlignment="Right" Margin="0" Width="106"/> </Grid> <TextBox Grid.Row="1" x:Name="_tbContent" IsReadOnly="True" AcceptsReturn="True" FontFamily="Courier New" Background="White" /> </Grid> </Grid> </Window> |
You can create an empty zip file using the Create method of C1ZipFile class. The code snippet shows handling a button event and calling the Create method within the event.
Please note that an UpdateDisplay utility function has been added to display the contents of the zip file. Within the UpdateDisplay method, a C1ZipEntry object has been declared to use it in a ForEach loop over the entries in the zip file.
For each entry, the function creates a ListViewItem containing the information extracted from the C1ZipEntry object and adds the new entry to the ListView control. The C1ZipEntry object itself is also saved in the Tag property of the ListView items.The code enables or disables the command buttons depending on whether or not there are entries available to be extracted, removed or tested.
An existing zip file on the disk can be opened using the Open method in C1ZipFile class. The code snippet shows handling a button event and calling the Open method within the event.
The C1.Zip library provides the CheckCRC32 method of C1ZipEntry class to check the integrity of the entries in the zip file. If the calculated checksum does not match the stored checksum, then either the zip file is corrupted or the program used to create the zip file is incompatible with C1Zip.
The user can view the contents of entry in a Zip file by calling the OpenReader method of C1ZipEntry class that can be used to read the content of the entry without extracting it into a disk file. The method also reads the entry content directly into a string using the ReadToEnd method on the StreamReader class, then closes the stream.
The user can add an entry into the current zip file by calling the Add method of C1ZipEntryCollection class. The C1.Zip library provides many overloads for the Add method, thereby enhancing the readability of the code. Here, we have called the Add(string fileName) overload method that uses the parameter fileName, which denotes the name of the file to add to the zip file. In the code snippet, the Add method is evoked on the Entries property of the C1ZipFile object, passing a string that contains the full name of the file to be added.
You can extract files from the current zip file using the Extract method of C1ZipEntryCollection class. The Extract method has many overloads, so that the user can use different data types and parameters conveniently to extract a file from zip archive. Here, we have used the Extract(string entryName) overload method, which has the parameter entryName, which denotes the name of the entry to extract. As you can see from the code snippet, the Extract method is called on the Entries property of the C1ZipFile object. By default, the Extract method expands the entry and saves it in the same directory where the zip file is located.
The C1.Zip library enables the user to remove an entry from the current zip file. The service library provides the Remove method of C1ZipEntryCollection class. The method has many overloads, thereby enhancing the readability of the code. Here, we have used the Remove(string fileName) method overload, which uses the fileName parameter or the name of the entry to remove from the zip archive.
From the code snippet, it is evident that the Remove method is called on the Entries property of the C1ZipFile object.