ComponentOne Zip for .NET
Medium Level: C1ZStreamReader and C1ZStreamWriter Classes
Zip for .NET Fundamentals > Medium Level: C1ZStreamReader and C1ZStreamWriter Classes

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 an ADO.NET DataTable object into a stream and then reads it back:

To write code in Visual Basic

Visual Basic
Copy Code
' Save the DataTable into a compressed stream.
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim fout As New FileStream("test.tmp", FileMode.Create)
bf.Serialize(fout, DataTableOut)
fout.Close()
' Read the compressed data.
Dim fin As New FileStream("test.tmp", FileMode.Open)
Dim DataTableIn As DataTable = bf.Deserialize(fin)

To write code in C#

C#
Copy Code
// Save the DataTable into compressed stream.
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
FileStream fout = new FileStream("test.tmp", FileMode.Create);
bf.Serialize(fout, DataTableOut);
fout.Close();
// Read the compressed data.
FileStream fin = new FileStream("test.tmp", FileMode.Open);
DataTable DataTableIn = (DataTable)bf.Deserialize(fin);

To add data compression, you would simply add two lines of code:

To write code in Visual Basic

Visual Basic
Copy Code
' Save the DataTable into a compressed stream.
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim fout As New FileStream("test.tmp", FileMode.Create)
Dim compressor As New C1ZStreamWriter(fout)
bf.Serialize(compressor, DataTableOut)
fout.Close()
' Read the compressed data.
Dim fin As New FileStream("test.tmp", FileMode.Open)
Dim decompressor As New C1ZStreamReader(fin)
Dim DataTableIn As DataTable = bf.Deserialize(decompressor)

To write code in C#

C#
Copy Code
// Save the DataTable into a compressed stream.
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
FileStream fout = new FileStream("test.tmp", FileMode.Create);
C1ZStreamWriter compressor = new C1ZStreamWriter(fout);
bf.Serialize(compressor, DataTableOut);
fout.Close();
// Read compressed data.
FileStream fin = new FileStream("test.tmp", FileMode.Open);
C1ZStreamReader decompressor = new C1ZStreamReader(fin);
DataTable DataTableIn = (DataTable)bf.Deserialize(decompressor);