ComponentOne Zip for .NET
Zip for .NET Task-Based Help / Saving a String Variable to a Zip File
In This Topic
    Saving a String Variable to a Zip File
    In This Topic

    To save a string variable to a zip file, use one of the following methods:

    The following code shows both methods. Add the code to the Button_Click event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.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 New C1ZipFile()
        zipFile.Create("c:\temp\strings.zip")
         ' Method 1: OpenWriter.
        Dim stream As Stream = zipFile.Entries.OpenWriter("Shakespeare.txt", True) 
        Dim sw As New StreamWriter(stream)
        sw.Write(str)
        sw.Close()
          ' 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
    

    To write code in C#

    C#
    Copy Code
    private void button1_Click(object sender, System.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();
         // 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();
    }