This quick start will guide you through the steps of creating a Zip file and adding entries to a Zip file using the C1ZipFile class.
Follow the steps below to create a zip file and add entries to it:
Start a new Visual Studio project.
From the Toolbox, add two Button controls and a ListView control to the form, and arrange them as shown in the snapshot.
Button | Button.Name Property | Button.Text Property |
1 | btnNew | Create Zip File |
2 | btnAdd | Add Files |
In the Properties window, click on the ellipsis button next to the Columns property, for the ListVew control. The ColumnHeader Collection Editor dialog box appears.Click the Add button to add five ColumnHeaders, and then set the Text property of each column to File, Date, Size, Compressed, and Amount, respectively. Select OK to close the editor box.
Note that to view the column headers in the designer, set the View property to Details in the Properties window or select Details from the View drop-down box in the ListView Tasks menu.
For WPF applications, open the MainWindow.xaml and replace the existing XAML with the following code.
XAML |
Copy Code
|
---|---|
<Window x:Class="Quickstart_zip.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:Quickstart_zip" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Button Name="btnNew" Content="Create Zip File…" HorizontalAlignment="Left" Margin="52,75,0,0" VerticalAlignment="Top" Width="125" Height="45" Click="BtnNew_Click"/> <Button Name="btnAdd" Content="Add Files…" HorizontalAlignment="Left" Height="47" Margin="50,174,0,0" VerticalAlignment="Top" Width="127" Click="BtnAdd_Click"/> <ListView x:Name="listView1" HorizontalAlignment="Left" Height="340" Margin="209,44,0,0" VerticalAlignment="Top" Width="551"> <ListView.View> <GridView> <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> </Window> |
In the Solution Explorer window, right-click on References, and select the Add Reference menu option. Add the C1.Zip assembly from the list.
Switch to Code Editor. In the beginning of the file, add the following directives:
This makes the objects defined in the C1.Zip and System.IO assemblies visible to the project.
In the Code Editor, copy the following data member declaration:
This is the main object in this application, and it implements the methods used to create, open and manage zip files.
In the Form_Load event, add the following code:
This code creates a new C1ZipFile object and assigns it to the 'zip' member. Note that the C1ZipFile object cannot be used yet. It needs to be attached to an existing zip file first using the Open method (or to a new one using the Create method).
Handle the Click event for the Create Zip File command button by adding the following code:
After creating the file, the code calls the UpdateDisplay utility function to display the contents of the zip file.
Add the UpdateDisplay utility function to display the contents of the zip file:
The main lines start by declaring a C1ZipEntry object and using it in a ForEach loop over the entries in the zip file (m_Zip.Entries).
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. Note that the C1ZipEntry object itself is also saved in the Tag property of the ListView items.
Handle the Click event for the Add Files command button by adding the following code:
Run the application. Now, you will be able to create zip files and add entries into the zip archives.