ComponentOne Zip for .NET 2.0
Walkthrough / Saving String Variable to Zip Files
In This Topic
    Saving String Variable to Zip Files
    In This Topic

    C1Zip not only allows you to compress files or folders into zipped files, but also provides the required methods in its API to compress long strings into memory stream.

    A string variable can be saved to a zip file using one of the following methods:

    Methods Description
    OpenWriter method The OpenWriter Method of C1ZipEntryCollection class can be used to get a stream writer, write the string into it, and then close it. The data is compressed as you write it into the stream, and the whole stream is saved into the zip file when you close it.
    MemoryStream method The MemoryStream method helps to write the data into it, and then add the stream to the zip file. Note that this method requires a little more work than the OpenWriter method, but is still very manageable.

    Add the following code to the Button_Click event to save string using OpenWriter method:

    This is the C# Code for WinForms applications:

    C#
    Copy Code
    private void saveButton_Click(object sender, EventArgs e)
    {
        string str = "Shall I compare thee to a summer's day? " +
    "Thou art more lovely and more temperate. " +
    "Rough winds do shake the darling buds of May, " +
    "And summer's lease hath all too short a date.";
        C1ZipFile zipFile = new C1ZipFile();
        zipFile.Create(@"c:\temp\strings.zip");
        // Method 1: OpenWriter.
        Stream stream = zipFile.Entries.OpenWriter("shakespeare.txt", true);
        StreamWriter sw = new StreamWriter(stream);
        sw.Write(str);
        sw.Close();      
    }
    

    This is the VB Code for WinForms applications:

    VB
    Copy Code
    Private Sub saveButton_Click(sender As Object, e As EventArgs) Handles saveButton.Click
        Dim str As String = "Shall I compare thee to a summer's day? " & "Thou art more lovely and more temperate. " & "Rough winds do shake the darling buds of May, " & "And summer's lease hath all too short a date."
        Dim zipFile As C1ZipFile = New C1ZipFile()
        zipFile.Create("c:\temp\strings.zip")
        ' Method 1: OpenWriter.
        Dim stream As Stream = zipFile.Entries.OpenWriter("shakespeare.txt", True)
        Dim sw As StreamWriter = New StreamWriter(stream)
        sw.Write(str)
        sw.Close()
    End Sub
    

    This is the C# code for WPF applications:

    C#
    Copy Code
     private void SaveButton_Click(object sender, RoutedEventArgs e)
     {
         string str = "Shall I compare thee to a summer's day? " +
    "Thou art more lovely and more temperate. " +
    "Rough winds do shake the darling buds of May, " +
    "And summer's lease hath all too short a date.";
         C1ZipFile zipFile = new C1ZipFile();
         zipFile.Create(@"c:\temp\strings.zip");
         // Method 1: OpenWriter.
         Stream stream = zipFile.Entries.OpenWriter("shakespeare.txt", true);
         StreamWriter sw = new StreamWriter(stream);
         sw.Write(str);
         sw.Close();
     }
    

    This is the VB code for WPF applications:

    VB
    Copy Code
    Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim str As String = "Shall I compare thee to a summer's day? " & "Thou art more lovely and more temperate. " & "Rough winds do shake the darling buds of May, " & "And summer's lease hath all too short a date."
        Dim zipFile As C1ZipFile = New C1ZipFile()
        zipFile.Create("c:\temp\strings.zip")
        ' Method 1: OpenWriter.
        Dim stream As Stream = zipFile.Entries.OpenWriter("shakespeare.txt", True)
        Dim sw As StreamWriter = New StreamWriter(stream)
        sw.Write(str)
        sw.Close()
    End Sub
    

    Add the following code to the Button_Click event to save string using MemoryStream method:

    This is the C# Code for WinForms applications:

    C#
    Copy Code
     private void save_Click(object sender, EventArgs e)
     {
         string str = "Shall I compare thee to a summer's day? " +
    "Thou art more lovely and more temperate. " +
    "Rough winds do shake the darling buds of May, " +
    "And summer's lease hath all too short a date.";
         C1ZipFile zipFile = new C1ZipFile();
         zipFile.Create(@"c:\temp\string.zip");
         // Method 2: Memory Stream.
         stream = new MemoryStream();
         sw = new StreamWriter(stream);
         sw.Write(str);
         sw.Flush();
         stream.Position = 0;
         zipFile.Entries.Add(stream, "Shakespeare2.txt");
         stream.Close();
     }
    

    This is the VB Code for WinForms applications:

    VB
    Copy Code
    Private Sub save_Click(sender As Object, e As EventArgs) Handles save.Click
        Dim str As String = "Shall I compare thee to a summer's day? " & "Thou art more lovely and more temperate. " & "Rough winds do shake the darling buds of May, " & "And summer's lease hath all too short a date."
        Dim zipFile As C1ZipFile = New C1ZipFile()
        zipFile.Create("c:\temp\string.zip")
        ' Method 2: Memory Stream.
        stream = New MemoryStream()
        sw = New StreamWriter(stream)
        sw.Write(str)
        sw.Flush()
        stream.Position = 0
        zipFile.Entries.Add(stream, "Shakespeare2.txt")
        stream.Close()
    End Sub
    

    This is the C# code for WPF applications:

    C#
    Copy Code
     private void Save_Click(object sender, RoutedEventArgs e)
     {
         string str = "Shall I compare thee to a summer's day? " +
    "Thou art more lovely and more temperate. " +
    "Rough winds do shake the darling buds of May, " +
    "And summer's lease hath all too short a date.";
         C1ZipFile zipFile = new C1ZipFile();
         zipFile.Create(@"c:\temp\string.zip");
         // Method 2: Memory Stream.
         stream = new MemoryStream();
         sw = new StreamWriter(stream);
         sw.Write(str);
         sw.Flush();
         stream.Position = 0;
         zipFile.Entries.Add(stream, "Shakespeare2.txt");
         stream.Close();
     }
    

    This is the VB code for WPF applications:

    VB
    Copy Code
    Private Sub Save_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim str As String = "Shall I compare thee to a summer's day? " & "Thou art more lovely and more temperate. " & "Rough winds do shake the darling buds of May, " & "And summer's lease hath all too short a date."
        Dim zipFile As C1ZipFile = New C1ZipFile()
        zipFile.Create("c:\temp\string.zip")
        ' Method 2: Memory Stream.
        stream = New MemoryStream()
        sw = New StreamWriter(stream)
        sw.Write(str)
        sw.Flush()
        stream.Position = 0
        zipFile.Entries.Add(stream, "Shakespeare2.txt")
        stream.Close()
    End Sub