In-memory stream compression and decompression
Sometimes, you need to perform some in-memory compression of a large amount of text. You might want to write this to a file or a database. Perhaps you need to e-mail the text as an attachment that another system will pick up and then decompress. Whatever the reason, in-memory compression and decompression is a very useful feature to have around. The best way to do this is to use extension methods. If you haven't figured this out by now, I quite like using extension methods.
Getting ready
The code is very straightforward. There is not much you will need to get ready beforehand. Just make sure that you include the following using
statements in your project and that you have a file containing text called file 3.txt
at the following path C:\temp\Documents\file 3.txt
. You can continue using the console application created in the preceding recipe.
using System.IO.Compression; using System.Text; using static System.Console;
How to do it...
- Create a class...