[]
        
(Showing Draft Content)

Handling Zip files

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.

The image shows an application with buttons and listview, showing basic operations of Zip library.

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.

<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>

Creating Zip Files

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.

WinForms

This is the C# Code for handling zip files in WinForms applications:

private void btnNew_Click(object sender, EventArgs e)
{
    // Show open file dialog. 
    SaveFileDialog fo = new SaveFileDialog();
    fo.FileName = "*.zip";
    if (fo.ShowDialog() != DialogResult.OK) return;
    // Open zip file. 
    try
    {
        m_Zip.Create(fo.FileName);
    }
    catch
    {
        MessageBox.Show("Can't create ZIP file, please try again.", "C1Zip");
    }
    // Update display. 
    UpdateDisplay();
}

This is the VB Code for handling zip files in WinForms applications:

Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
    'Show save file dialog
    Dim fo As SaveFileDialog = New SaveFileDialog()
    fo.FileName = "*.zip"
    If fo.ShowDialog() <> DialogResult.OK Then Return
    'Create zip file
    Try
        zip.Create(fo.FileName)
    Catch
        MessageBox.Show("Can't create ZIP file, please try again.", "C1Zip")
    End Try
    ' Update display
    UpdateDisplay()
End Sub

WPF

This is the C# code for handling zip files in WPF applications:

void BtnNew_Click(object sender, RoutedEventArgs e)
{
    // Create the C1ZipFile member.
    zip = new C1ZipFile();
    // Show open file dialog. 
    SaveFileDialog fo = new SaveFileDialog();
    fo.Filter = "Zip Files (*.zip) | *.zip";
    var open = fo.ShowDialog();
    if (open.HasValue && open.Value)
    {
        try
        {
            // Create zip file. 
            zip.Create(fo.FileName);
        }
        catch
        {
            MessageBox.Show("Can't create ZIP file, please try again.", "C1Zip");
        }
    }
}

This is the VB code for handling zip files in WPF applications:

Private Sub BtnNew_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    ' Create the C1ZipFile member.
    zip = New C1ZipFile()
    ' Show open file dialog. 
    Dim fo As SaveFileDialog = New SaveFileDialog()
    fo.Filter = "Zip Files (*.zip) | *.zip"
    Dim open = fo.ShowDialog()
    If open.HasValue AndAlso open.Value Then
        Try
            ' Create zip file. 
            zip.Create(fo.FileName)
        Catch
            MessageBox.Show("Can't create ZIP file, please try again.", "C1Zip")
        End Try
    End If
End Sub

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.

WinForms

This is the C# Code for handling zip files in WinForms applications:

private void UpdateDisplay()
{
    // Remove any existing items.
    listView1.Items.Clear();
    // Add each entry.
    foreach (C1ZipEntry ze in m_Zip.Entries)
    {
        // Calculate the compression amount.
        double pct = 0;
        if (ze.SizeUncompressed > 0)
        {
            pct = 1 - (((double)ze.SizeCompressed) / ((double)ze.SizeUncompressed));
        }
        // Build ListView item.
        ListViewItem lvi = new ListViewItem(new string[] { ze.FileName, Microsoft.VisualBasic.Strings.Format(ze.Date, "MM/dd/yy"), Microsoft.VisualBasic.Strings.Format(ze.SizeUncompressed, "#,##0"), Microsoft.VisualBasic.Strings.Format(ze.SizeCompressed, "#,##0"), Microsoft.VisualBasic.Strings.Format(pct, "00 %") });
        // Save ZipEntry into item tag.
        lvi.Tag = ze;
        // Add item to ListView.
        listView1.Items.Add(lvi);
    }
    // Update UI.
    bool hasEntries = (listView1.Items.Count > 0);
    btnExtract.Enabled = hasEntries;
    btnRemove.Enabled = hasEntries;
    btnTest.Enabled = hasEntries;
}

This is the VB Code for handling zip files in WinForms applications:

Private Sub UpdateDisplay()
    ' Remove any existing items.
    listView1.Items.Clear()
    ' Add each entry.
    For Each ze As C1ZipEntry In zip.Entries
        Dim pct As Double = 0
        ' Calculate the compression amount.
        If ze.SizeUncompressed > 0 Then
            pct = 1 - ((CDbl(ze.SizeCompressed)) / (CDbl(ze.SizeUncompressed)))
        End If
        ' Build ListView item.
        Dim lvi As ListViewItem = New ListViewItem(New String() {ze.FileName, Microsoft.VisualBasic.Strings.Format(ze.Date, "MM/dd/yy"), Microsoft.VisualBasic.Strings.Format(ze.SizeUncompressed, "#,##0"), Microsoft.VisualBasic.Strings.Format(ze.SizeCompressed, "#,##0"), Microsoft.VisualBasic.Strings.Format(pct, "00 %")})
        ' Save ZipEntry into item tag.
        lvi.Tag = ze
        listView1.Items.Add(lvi)
    Next
    ' Update UI.
    Dim hasEntries As Boolean = (listView1.Items.Count > 0)
    btnExtract.Enabled = hasEntries
    btnRemove.Enabled = hasEntries
    btnTest.Enabled = hasEntries
End Sub

WPF

This is the C# code for handling zip files in WPF applications:

void UpdateDisplay()
{
    var sel = listView1.SelectedItem;
    listView1.ItemsSource = null;
    if (zip == null)
    {
        return;
    }
    listView1.ItemsSource = zip.Entries;
    if (zip.Entries.Count == 0)
    {               
        btnRemove.IsEnabled = false;
        btnView.IsEnabled = false;                
        btnTest.IsEnabled = false;
    }
    listView1.SelectedItem = sel;
}

This is the VB code for handling zip files in WPF applications:

Private Sub UpdateDisplay()
    Dim sel = Me.listView1.SelectedItem
    Me.listView1.ItemsSource = Nothing
    If zip Is Nothing Then
        Return
    End If
    Me.listView1.ItemsSource = zip.Entries
    If zip.Entries.Count = 0 Then
        Me.btnRemove.IsEnabled = False
        Me.btnView.IsEnabled = False
        Me.btnTest.IsEnabled = False
    End If
    Me.listView1.SelectedItem = sel
End Sub

Opening Existing Zip Files

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.

WinForms

This is the C# Code for handling zip files in WinForms applications:

private void btnOpen_Click(object sender, EventArgs e)
{
    // Show open file dialog.
    OpenFileDialog fo = new OpenFileDialog();
    fo.FileName = "*.zip";
    if (fo.ShowDialog() != DialogResult.OK) return;
    try
    {
        m_Zip.Open(fo.FileName);
    }
    catch
    {
        MessageBox.Show("Invalid ZIP file, please try again.");
    }
    // Update display.
    UpdateDisplay();
}

This is the VB Code for handling zip files in WinForms applications:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
    ' Show open file dialog.
    Dim fo As OpenFileDialog = New OpenFileDialog()
    fo.FileName = "*.zip"
    If fo.ShowDialog() <> DialogResult.OK Then Return
    Try
        zip.Open(fo.FileName)
    Catch
        MessageBox.Show("Invalid ZIP file, please try again.")
    End Try
    ' Update display
    UpdateDisplay()
End Sub

WPF

This is the C# code for handling zip files in WPF applications:

private void BtnOpen_Click(object sender, RoutedEventArgs e)
{
    // Create the C1ZipFile member.
    zip = new C1ZipFile();
    Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog();
    op.Filter = "Zip(*.zip)|*.zip";
    var open = op.ShowDialog();
    if (open.HasValue && open.Value)
    {
        if (zip == null)
        {
            zip = new C1ZipFile();
        }
        zip.Open(op.FileName);
        btnExtract.IsEnabled = true;
        UpdateDisplay();
    }
}

This is the VB code for handling zip files in WPF applications:

Private Sub BtnOpen_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    ' Create the C1ZipFile member.
    zip = New C1ZipFile()
    Dim op As OpenFileDialog = New OpenFileDialog()
    op.Filter = "Zip(*.zip)|*.zip"
    Dim open = op.ShowDialog()
    If open.HasValue AndAlso open.Value Then
        If zip Is Nothing Then
            zip = New C1ZipFile()
        End If
        zip.Open(op.FileName)
        Me.btnExtract.IsEnabled = True
        UpdateDisplay()
    End If
End Sub

Testing Integrity of Zip File

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.

WinForms

This is the C# Code for handling zip files in WinForms applications:

private void BtnTest_Click(object sender, RoutedEventArgs e)
{
    // Create the C1ZipFile member.
    //  zip = new C1ZipFile();
    // Test each entry.
    foreach (C1ZipEntry ze in zip.Entries)
    {
        if (!ze.CheckCRC32())
        {
            MessageBox.Show("** Entry " + ze.FileName + " has errors.", "C1Zip", MessageBoxButton.OK, MessageBoxImage.Error);
            return;
        }
    }
    // If we got here, everything is OK.
    MessageBox.Show("All entries passed CRC check", "C1Zip");
}

This is the VB Code for handling zip files in WinForms applications:

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles btnTest.Click
    For Each ze As C1ZipEntry In zip.Entries
        ' Test each entry
            If Not ze.CheckCRC32() Then
            MessageBox.Show("** Entry " & ze.FileName & " has errors.", "C1Zip", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Return
        End If
    Next
    ' If we got here, everything is OK. 
        MessageBox.Show("All entries passed CRC check", "C1Zip")
End Sub
End Class

WPF

This is the C# code for handling zip files in WPF applications:

private void BtnTest_Click(object sender, RoutedEventArgs e)
{
    // Create the C1ZipFile member.
    //  zip = new C1ZipFile();
    // Test each entry.
    foreach (C1ZipEntry ze in zip.Entries)
    {
        if (!ze.CheckCRC32())
        {
            MessageBox.Show("** Entry " + ze.FileName + " has errors.", "C1Zip", MessageBoxButton.OK, MessageBoxImage.Error);
            return;
        }
    }
    // If we got here, everything is OK.
    MessageBox.Show("All entries passed CRC check", "C1Zip");
}

This is the VB code for handling zip files in WPF applications:

Private Sub BtnTest_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    ' Create the C1ZipFile member.
    '  zip = new C1ZipFile();
    ' Test each entry.
    For Each ze As C1ZipEntry In zip.Entries
        If Not ze.CheckCRC32() Then
            MessageBox.Show("** Entry " & ze.FileName & " has errors.", "C1Zip", MessageBoxButton.OK, MessageBoxImage.Error)
            Return
        End If
    Next
    ' If we got here, everything is OK.
    MessageBox.Show("All entries passed CRC check", "C1Zip")
End Sub
' close the preview pane by hiding it
Private Sub _btnClosePreview_Click_1(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Me._preview.Visibility = Visibility.Collapsed
    Me._mainpage.Visibility = Visibility.Visible
End Sub

Viewing Contents of Entry in ‬Zip File

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.

WinForms

This is the C# Code for handling zip files in WinForms applications:

private void btnView_Click(object sender, EventArgs e)
{
    // Get the first selected item that is not a directory.
    C1ZipEntry ze = null;
    foreach (ListViewItem lvi in listView1.SelectedItems)
    {
        C1ZipEntry zeItem = lvi.Tag as C1ZipEntry;
        if ((zeItem.Attributes == 0) && (FileAttributes.Directory == 0))
        {
            ze = zeItem;
            break;
        }
    }
    // Make sure we got something.
    if (ze == null)
    {
        MessageBox.Show("Sorry, no files to show...", "C1Zip");
        return;
    }
    // Read entry content into a string.
    Stream entry = ze.OpenReader();
    StreamReader sr = new StreamReader(entry);
    string entryText = sr.ReadToEnd();
    entry.Close();
    // Make sure the entry is not too big for the MessageBox.
    if (entryText.Length > 16000)
    {
        entryText = entryText.Substring(0, 16000);
    }
    // Show the entry in the message box.
    MessageBox.Show(entryText, ze.FileName);
}

This is the VB Code for handling zip files in WinForms applications:

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles btnView.Click
    ' Get the first selected item that is not a directory
    Dim ze As C1ZipEntry = Nothing
    For Each lvi As ListViewItem In listView1.SelectedItems
        Dim zeItem As C1ZipEntry = TryCast(lvi.Tag, C1ZipEntry)
        If (zeItem.Attributes = 0) AndAlso (FileAttributes.Directory = 0) Then
            ze = zeItem
            Exit For
        End If
    Next
    ' Make sure we got something
    If ze Is Nothing Then
        MessageBox.Show("Sorry, no files to show...", "C1Zip")
        Return
    End If
    ' Read entry content into a string.
    Dim entry As Stream = ze.OpenReader()
    Dim sr As StreamReader = New StreamReader(entry)
    Dim entryText As String = sr.ReadToEnd()
    entry.Close()
    ' Make sure the entry is not too big for the MessageBox.
    If entryText.Length > 16000 Then
        entryText = entryText.Substring(0, 16000)
    End If
    ' Show the entry in the message box.
    MessageBox.Show(entryText, ze.FileName)
End Sub

WPF

This is the C# code for handling zip files in WPF applications:

void BtnView_Click(object sender, RoutedEventArgs e)
{
    var entry = listView1.SelectedItem as C1ZipEntry;
    if (entry != null)
    {
        using (var stream = entry.OpenReader())
        {
            var sr = new System.IO.StreamReader(stream);
            _tbContent.Text = sr.ReadToEnd();
        }
        _preview.Visibility = Visibility.Visible;
        _mainpage.Visibility = Visibility.Collapsed;
    }
}

This is the VB code for handling zip files in WPF applications:

Private Sub BtnView_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim entry = TryCast(Me.listView1.SelectedItem, C1ZipEntry)
    If entry IsNot Nothing Then
        Using stream = entry.OpenReader()
            Dim sr = New StreamReader(stream)
            Me._tbContent.Text = sr.ReadToEnd()
        End Using
        Me._preview.Visibility = Visibility.Visible
        Me._mainpage.Visibility = Visibility.Collapsed
    End If
End Sub

Adding Entries to Zip File

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.

WinForms

This is the C# Code for handling zip files in WinForms applications:

private void btnAdd_Click(object sender, EventArgs e)
{
    // Get list of files to add.
    OpenFileDialog fo = new OpenFileDialog();
    fo.Multiselect = true;
    fo.FileName = "*.*";
    if (fo.ShowDialog() == DialogResult.OK)
    {
        // Add files in the list.
        foreach (string file in fo.FileNames)
        {
            m_Zip.Entries.Add(fo.FileName);
        }
        // Done.
        UpdateDisplay();
    }
}

This is the VB Code for handling zip files in WinForms applications:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    ' Get list of files to add
    Dim fo As OpenFileDialog = New OpenFileDialog()
    fo.Multiselect = True
    fo.FileName = "*.*"
    If fo.ShowDialog() = DialogResult.OK Then
        ' Add files in the list.
        For Each file As String In fo.FileNames
            zip.Entries.Add(fo.FileName)
        Next
        ' Done
        UpdateDisplay()
    End If
End Sub

WPF

This is the C# code for handling zip files in WPF applications:

private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
    // Get list of files to add.
    OpenFileDialog fo = new OpenFileDialog();
    fo.Multiselect = true;
    fo.FileName = "*.*";
    var open = fo.ShowDialog();
    if (open.HasValue && open.Value)
    {
        // Add files in the list.
        foreach (string file in fo.FileNames)
        {
            zip.Entries.Add(file);
        }
        // Done.
        UpdateDisplay();
    }
}

This is the VB code for handling zip files in WPF applications:

Private Sub BtnAdd_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    ' Get list of files to add.
    Dim fo As OpenFileDialog = New OpenFileDialog()
    fo.Multiselect = True
    fo.FileName = "*.*"
    Dim open = fo.ShowDialog()
    If open.HasValue AndAlso open.Value Then
        ' Add files in the list.
        For Each file As String In fo.FileNames
            zip.Entries.Add(file)
        Next
        ' Done.
        UpdateDisplay()
    End If
End Sub

Extracting Entries from Zip File

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.

WinForms

This is the C# Code for handling zip files in WinForms applications:

private void btnExtract_Click(object sender, EventArgs e)
{
    // Make sure we have some selected entries.
    int cnt = listView1.SelectedIndices.Count;
    if (cnt == 0)
    {
        MessageBox.Show("Sorry, no files to extract...", "C1Zip");
        return;
    }
    // Confirm with user.
    DialogResult dr;
    string msg;
    msg = "Please confirm that you want to extract " + cnt.ToString() + " entries.";
    dr = MessageBox.Show(msg, "C1Zip", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
    if (dr != DialogResult.OK) return;
    // Extract all selected entries.
    C1ZipEntry ze;
    foreach (ListViewItem lvi in listView1.SelectedItems)
    {
        ze = (C1ZipEntry)lvi.Tag;
        if (ze.SizeCompressed > 0)
        {
                m_Zip.Entries.Extract(ze.FileName);                    
        }
    }
    // Done.
    UpdateDisplay();
}

This is the VB Code for handling zip files in WinForms applications:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnExtract.Click
    ' Make sure we have some selected entries.
    Dim cnt As Integer = listView1.SelectedIndices.Count
    If cnt = 0 Then
        MessageBox.Show("Sorry, no files to extract...", "C1Zip")
        Return
    End If
    ' Confirm with user
    Dim dr As DialogResult
    Dim msg As String
    msg = "Please confirm that you want to extract " & cnt.ToString() & " entries."
    dr = MessageBox.Show(msg, "C1Zip", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
    If dr <> DialogResult.OK Then Return
    ' Extract selected entries
    Dim ze As C1ZipEntry
    ' Add files in the list
    For Each lvi As ListViewItem In listView1.SelectedItems
        ze = CType(lvi.Tag, C1ZipEntry)
        If ze.SizeCompressed > 0 Then
            zip.Entries.Extract(ze.FileName)
        End If
    Next
    ' Done
    UpdateDisplay()
End Sub

WPF

This is the C# code for handling zip files in WPF applications:

private void BtnExtract_Click(object sender, RoutedEventArgs e)
{
    // Make sure we have some selected entries.
    int cnt = listView1.SelectedItems.Count;
    if (cnt == 0)
    {
        MessageBox.Show("Sorry, no files to extract...", "C1Zip");
        return;
    }
    // Confirm with user.
    MessageBoxResult dr;
    string msg;
    msg = "Please confirm that you want to extract " + cnt.ToString() + " entries.";
    dr = MessageBox.Show(msg, "C1Zip", MessageBoxButton.OKCancel, MessageBoxImage.Question);
    if (dr != MessageBoxResult.OK) return;
    System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
    dlg.ShowDialog();
    // Extract all selected entries.
    foreach (C1ZipEntry c1ze in zip.Entries)
    {
        var dstFileName = dlg.SelectedPath + @"\" + c1ze.FileName;
        var dstFolder = System.IO.Path.GetDirectoryName(dstFileName);
        if (dstFolder.Length > 0 && !Directory.Exists(dstFolder))
        {
            // Extract all selected entries.
            Directory.CreateDirectory(dstFolder);
            var dstName = System.IO.Path.GetFileName(dstFileName);
            if (dstName.Length > 0)
            {
                if (c1ze.SizeCompressed > 0)
                    c1ze.Extract(dstFileName);
            }
        }
        else
        {
            c1ze.Extract(dstFileName);
        }
    }
    // Done
    UpdateDisplay();
}

This is the VB code for handling zip files in WPF applications:

Private Sub BtnExtract_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    ' Make sure we have some selected entries.
    Dim cnt As Integer = Me.listView1.SelectedItems.Count
    If cnt = 0 Then
        MessageBox.Show("Sorry, no files to extract...", "C1Zip")
        Return
    End If
    ' Confirm with user.
    Dim dr As MessageBoxResult
    Dim msg As String
    msg = "Please confirm that you want to extract " & cnt.ToString() & " entries."
    dr = MessageBox.Show(msg, "C1Zip", MessageBoxButton.OKCancel, MessageBoxImage.Question)
    If dr <> MessageBoxResult.OK Then Return
    Dim dlg As FolderBrowserDialog = New FolderBrowserDialog()
    dlg.ShowDialog()
    ' Extract all selected entries.
    For Each c1ze As C1ZipEntry In zip.Entries
        Dim dstFileName = dlg.SelectedPath & "\" & c1ze.FileName
        Dim dstFolder = IO.Path.GetDirectoryName(dstFileName)
        If dstFolder.Length > 0 AndAlso Not Directory.Exists(dstFolder) Then
            ' Extract all selected entries.
            Directory.CreateDirectory(dstFolder)
            Dim dstName = IO.Path.GetFileName(dstFileName)
            If dstName.Length > 0 Then
                If c1ze.SizeCompressed > 0 Then c1ze.Extract(dstFileName)
            End If
        Else
            c1ze.Extract(dstFileName)
        End If
    Next
    ' Done
    UpdateDisplay()
End Sub

Removing Entries from Zip file

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.

WinForms

This is the C# Code for handling zip files in WinForms applications:

private void btnRemove_Click(object sender, EventArgs e)
{
    // Make sure we have some selected entries.
    int cnt = listView1.SelectedIndices.Count;
    if (cnt == 0)
    {
        MessageBox.Show("Oops, no files to remove...", "C1Zip");
        return;
    }
    // Confirm with user.
    DialogResult dr;
    string msg;
    msg = "Please confirm that you want to delete " + cnt.ToString() + " entries.";
    dr = MessageBox.Show(msg, "C1Zip", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
    if (dr != DialogResult.OK) return;
    // Delete all selected entries.
    foreach (ListViewItem lvi in listView1.SelectedItems)
    {
        C1ZipEntry ze = (C1ZipEntry)lvi.Tag;
        if (ze.SizeCompressed > 0)
        {
            m_Zip.Entries.Remove(ze.FileName);
        }
    }
    // Done.
    UpdateDisplay();
}

This is the VB Code for handling zip files in WinForms applications:

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    ' Make sure we have some selected entries
    Dim cnt As Integer = listView1.SelectedIndices.Count
    If cnt = 0 Then
        MessageBox.Show("Oops, no files to remove...", "C1Zip")
        Return
    End If
    ' Confirm with user.
    Dim dr As DialogResult
    Dim msg As String
    msg = "Please confirm that you want to delete " & cnt.ToString() & " entries."
    dr = MessageBox.Show(msg, "C1Zip", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
    If dr <> DialogResult.OK Then Return
    ' Delete all selected entries.
    For Each lvi As ListViewItem In listView1.SelectedItems
        Dim ze As C1ZipEntry = CType(lvi.Tag, C1ZipEntry)
        If ze.SizeCompressed > 0 Then
            zip.Entries.Remove(ze.FileName)
        End If
    Next
    ' Done
    UpdateDisplay()
End Sub

WPF

This is the C# code for handling zip files in WPF applications:

void BtnRemove_Click(object sender, RoutedEventArgs e)
{
    foreach (C1ZipEntry entry in listView1.SelectedItems)
    {
        zip.Entries.Remove(entry.FileName);
    }
    // Done 
    UpdateDisplay();
}

This is the VB code for handling zip files in WPF applications:

Private Sub BtnRemove_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    For Each entry As C1ZipEntry In Me.listView1.SelectedItems
        zip.Entries.Remove(entry.FileName)
    Next
    ' Done 
    UpdateDisplay()
End Sub

See Also

Quickstart

Walkthrough