Zip for WPF and Silverlight
C1Zip Fundamentals / Medium Level: C1ZStreamReader and C1ZStreamWriter Classes
In This Topic
    Medium Level: C1ZStreamReader and C1ZStreamWriter Classes
    In This Topic

    The C1ZStreamReader and C1ZStreamWriter classes allow you to use data compression on any .NET streams, not only in zip files.

    To use C1ZStreamReader and C1ZStreamWriter objects, attach them to regular streams and read or write the data through them. The data is compressed (or expanded) on the fly into (or out of) the underlying stream.

    This design allows great integration with native .NET streams. The diagram below illustrates how it works:

     

     

    For example, the code below saves a DataGrid into a stream and then reads it back:

    C#
    Copy Code
    private const string FN_REGULAR = @"\regular";
            private const string FN_COMPRESSED = @"\compressed";
            PersonList personList = new PersonList();
            public MainWindow()
            {
                InitializeComponent();
            }
            private void btnCreate_Click(object sender, RoutedEventArgs e)
            {
                for (int i = 0; i < 1000; i++)
                {
                    personList.Persons.Add(new Person()
                    {
                        FirstName = string.Format("First Name {0}", i),
                        LastName = string.Format("Last Name {0}", i),
                        Age = i,
                        City = string.Format("City {0}", i)
                    });
                }
                this.dataGrid1.ItemsSource = personList.Persons;
            }
            private void btnSave_Click(object sender, RoutedEventArgs e)
            {
                // Show status.   
                Cursor = Cursors.Wait;   
                label1.Content = "Serializing data to regular file..."; 
                // Serialize the data set to a regular file.   
                string fn = Environment.CurrentDirectory + FN_REGULAR; 
                FileStream fs = new FileStream(fn, FileMode.Create);   
                BinaryFormatter bf = new BinaryFormatter();   
                bf.Serialize(fs, personList);   
                long lenRegular = fs.Length;   
                fs.Close();    
                // Show status.   
                Cursor = Cursors.Wait;
                label1.Content = "Serializing data to compressed file...";            
                // Serialize the data set to a compressed file.   
                fn = Environment.CurrentDirectory + FN_COMPRESSED;   
                fs = new FileStream(fn, FileMode.Create);   
                C1ZStreamWriter compressor = new C1ZStreamWriter(fs);   
                bf = new BinaryFormatter();   
                bf.Serialize(compressor, personList);   
                long lenCompressed = fs.Length;   
                fs.Close();    
                // Show status.   
                Cursor = Cursors.Hand;   
                label1.Content = string.Format("Saved to regular file ({0:#,###} bytes) and " + "compressed file ({1:#,###} bytes)", lenRegular, lenCompressed);  
                // Enable the load buttons.   
                btnLoad.IsEnabled = true;
                btnLoadCompressed.IsEnabled = true;
            }
            private void btnLoad_Click(object sender, RoutedEventArgs e)
            {
                // Clear grid, show status.   
                Cursor = Cursors.Wait;
                dataGrid1.ItemsSource = null;          
                label1.Content = "Loading from regular file...";               
                // Deserialize from regular file.   
                string fn = Environment.CurrentDirectory + FN_REGULAR;   
                FileStream fs = new FileStream(fn, FileMode.Open);   
                long ticks = DateTime.Now.Ticks;   
                BinaryFormatter bf = new BinaryFormatter();              
                PersonList pl = (PersonList)bf.Deserialize(fs);   
                long ms = (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond;   
                fs.Close();              
                // Show result.   
                Cursor = Cursors.Hand;   
                dataGrid1.ItemsSource = pl.Persons;   
                label1.Content = "Loaded from regular file in " + ms.ToString() + " ms.";
            }
            private void btnLoadCompressed_Click(object sender, RoutedEventArgs e)
            {
                // Clear grid, show status.   
                Cursor = Cursors.Wait;   
                dataGrid1.ItemsSource = null;
                label1.Content = "Loading from compressed file...";
                // Deserialize from compressed file.   
                string fn = Environment.CurrentDirectory + FN_COMPRESSED;   
                FileStream fs = new FileStream(fn, FileMode.Open);   
                long ticks = DateTime.Now.Ticks;   
                C1ZStreamReader decompressor;   
                decompressor = new C1ZStreamReader(fs);   
                BinaryFormatter bf = new BinaryFormatter();
                PersonList pl = (PersonList)bf.Deserialize(decompressor);
                long ms = (DateTime.Now.Ticks - ticks) / TimeSpan.TicksPerMillisecond; fs.Close();               
                // Show result.   
                Cursor = Cursors.Hand;   
                dataGrid1.ItemsSource = pl.Persons;   
                label1.Content = "Loaded from compressed file in " + ms.ToString() + " ms.";
            }