Explain how to compress data with a compression Stream.
Compression streams write to another stream. The compression streams take in data like any other stream. But then it writes it in compressed format to another stream.
Steps to compress data using compression stream:
1. Open the file and create a new file for the compressed version
FileStream orgFile = File.OpenRead(@"C:\abc.bak");
FileStream compFile = File.Create(@"C:\abc.gzip");
2. Compression stream wraps the outgoing stream with the compression stream.
GZipStream compStream = new GZipStream(compFile, CompressionMode.Compress);
3. Write from the original file to compression stream
int wrtTxt = orgFile.ReadByte();
while (wrtTxt != -1)
{
compStream.WriteByte((byte) wrtTxt);
wrtTxt = orgFile.ReadByte();
}