NET - Explain how to compress data with a compression Stream

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();
}
NET - Explain how to Throw and Catch Exceptions in C#.NET
Answer - Explain how to Throw and Catch Exceptions......
NET - Explain when should we use StringBuilder class instead of the String class
Use StringBuilder class instead of the String class - Whenever any of the methods of String class is used to modify the string......
NET - Explain why we should close and dispose resources in a Finally block instead of Catch block
Explain why we should close and dispose resources in a Finally block instead of Catch block - Catch block gets called only when an exception occurs or is explicitly thrown.......
Post your comment