ComponentOne Zip for .NET 2.0
In This Topic
    Creating Zip Files with Multiple Entries
    In This Topic

    Multiple files or entries can be compressed into a single zipped file, and this can considerably reduce the file size and conserve space. The Zip library provides this utility so that you can easily create zip file with multiple entries. For example, this is beneficial while adding files as attachment while emailing.

    C1Zip provides the OpenWriter method of C1ZipEntryCollection class to stream multiple files directly into the zip file. Such files can be of any format, be it .xml, .docx, .pdf, .xlsx etc. This method returns a stream that you can write to, and when the stream is closed, it is added to the zip file.

    This is the C# Code for WinForms applications:

    C#
    Copy Code
    private void btnCreate_Click(object sender, EventArgs e)
    {
        C1ZipFile zip = new C1ZipFile();
        zip.Create(@"c:\temp\test.zip");
        Stream s = zip.Entries.OpenWriter("entry1", true);            
        StreamWriter sw = new StreamWriter(s);
        sw.WriteLine("Hello world");
        // Continue writing as much as you want...
        sw.Close();
        s = zip.Entries.OpenWriter("entry2", true);
        sw = new StreamWriter(s);
        sw.WriteLine("Hello again");
        // Continue writing as much as you want...
        sw.Close();
    }
    

    This is the VB Code for WinForms applications:

    VB
    Copy Code
    Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
        Dim zip As C1ZipFile = New C1ZipFile()
        zip.Create("c:\temp\test.zip")
        Dim s As Stream = zip.Entries.OpenWriter("entry1", True)
        Dim sw As StreamWriter = New StreamWriter(s)
        sw.WriteLine("Hello world")
        ' Continue writing as much as you want...
        sw.Close()
        s = zip.Entries.OpenWriter("entry2", True)
        sw = New StreamWriter(s)
        sw.WriteLine("Hello again")
        ' Continue writing as much as you want...
        sw.Close()
    End Sub
    

    This is the C# code for WPF applications:

    C#
    Copy Code
    private void BtnCreate_Click(object sender, RoutedEventArgs e)
    {
        C1ZipFile zip = new C1ZipFile();
        zip.Create(@"c:\temp\tests.zip");
        Stream s = zip.Entries.OpenWriter("entry1", true);
        StreamWriter sw = new StreamWriter(s);
        sw.WriteLine("Hello world");
        // Continue writing as much as you want...
        sw.Close();
        s = zip.Entries.OpenWriter("entry2", true);
        sw = new StreamWriter(s);
        sw.WriteLine("Hello again");
        // Continue writing as much as you want...
        sw.Close();
    }
    

    This is the VB code for WPF applications:

    VB
    Copy Code
    Private Sub BtnCreate_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim zip As C1ZipFile = New C1ZipFile()
        zip.Create("c:\temp\tests.zip")
        Dim s As Stream = zip.Entries.OpenWriter("entry1", True)
        Dim sw As StreamWriter = New StreamWriter(s)
        sw.WriteLine("Hello world")
        ' Continue writing as much as you want...
        sw.Close()
        s = zip.Entries.OpenWriter("entry2", True)
        sw = New StreamWriter(s)
        sw.WriteLine("Hello again")
        ' Continue writing as much as you want...
        sw.Close()
    End Sub
    

    The Zip library also provides the OpenReader method of C1ZipEntry class on the entry object to read an entry without extracting it to a file.

    This is the C# Code for WinForms applications:

    C#
    Copy Code
    private void read_btn_Click(object sender, EventArgs e)
    {
        // Open a zip file.
        C1ZipFile zip = new C1ZipFile();
        zip.Open(@"c:\temp\test.zip");
        // Open an input stream on any entry.
        C1ZipEntry ze = zip.Entries["file.cs"];
        Stream s = ze.OpenReader();
        // Open the StreamReader on the stream.
        StreamReader sr = new StreamReader(s);
        // Use the StreamReader, then close it.
    }
    

    This is the VB Code for WinForms applications:

    VB
    Copy Code
    Private Sub read_btn_Click(sender As Object, e As EventArgs) Handles read_btn.Click
        ' Open a zip file.
        Dim zip As C1ZipFile = New C1ZipFile()
        zip.Open("c:\temp\test.zip")
        ' Open an input stream on any entry.
        Dim ze As C1ZipEntry = zip.Entries("file.cs")
        Dim s As Stream = ze.OpenReader()
        ' Open the StreamReader on the stream.
        Dim sr As StreamReader = New StreamReader(s)
        ' Use the StreamReader, then close it.
    End Sub
    

    This is the C# code for WPF applications:

    C#
    Copy Code
    private void Read_btn_Click(object sender, RoutedEventArgs e)
    {
        // Open a zip file.
        C1ZipFile zip = new C1ZipFile();
        zip.Open(@"c:\temp\test.zip");
        // Open an input stream on any entry.
        C1ZipEntry ze = zip.Entries["file.cs"];
        Stream s = ze.OpenReader();
        // Open the StreamReader on the stream.
        StreamReader sr = new StreamReader(s);
        // Use the StreamReader, then close it.
    }
    

    This is the VB code for WPF applications:

    VB
    Copy Code
    Private Sub Read_btn_Click(sender As Object, e As RoutedEventArgs) Handles read_btn.Click
        ' Open a zip file.
        Dim zip As C1ZipFile = New C1ZipFile()
        zip.Open("c:\temp\test.zip")
        ' Open an input stream on any entry.
        Dim ze As C1ZipEntry = zip.Entries("file.cs")
        Dim s As Stream = ze.OpenReader()
        ' Open the StreamReader on the stream.
        Dim sr As StreamReader = New StreamReader(s)
        ' Use the StreamReader, then close it.
    End Sub
    
    See Also