ComponentOne Zip for UWP
Saving a String Variable to a Zip File
Zip for UWP Task-Based Help > Saving a String Variable to a Zip File

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

Use the C1ZipEntryCollection.OpenWriter method 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.

Use a MemoryStream method; 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 C1ZipEntryCollection.OpenWriter method, but is still very manageable.

The following code shows both methods. In this example, the code for the OpenWriter method is shown in the button1_Click event. The code for the MemoryStream method is shown in the button2_Click event.

C#
Copy Code
private void button1_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlgSaveFile = new SaveFileDialog();
            dlgSaveFile.Filter = "Zip Files (*.zip) | *.zip";
            if (dlgSaveFile.ShowDialog() == true)
            {
                zipFile.Create(dlgSaveFile.OpenFile());
            }
            // Method 1: OpenWriter.
            Stream stream = zipFile.Entries.OpenWriter("Shakespeare.txt", true);
            C1ZStreamWriter sw = new C1ZStreamWriter(stream);
            byte[] text = System.Text.Encoding.Unicode.GetBytes(shakespeareText);
            sw.Write(text, 0, text.Length);
            sw.Flush();
            stream.Close();
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dlgSaveFile = new SaveFileDialog();
            dlgSaveFile.Filter = "Zip Files (*.zip) | *.zip";
            if (dlgSaveFile.ShowDialog() == true)
            {
                zipFile.Create(dlgSaveFile.OpenFile());
            }
            // Method 2: Memory Stream.
            Stream stream = new MemoryStream();
            C1ZStreamWriter sw = new C1ZStreamWriter(stream);
            byte[] text = System.Text.Encoding.Unicode.GetBytes(shakespeareText);
            sw.Write(text, 0, text.Length);
            sw.Flush();
            stream.Position = 0;
            zipFile.Entries.Add(stream, "Shakespeare2.txt");
            stream.Close();
        }
See Also