Now I’m going to create a C# application that writes a lot of data to a compressed file. First, we’re going to write it to a standard file so we can
compare the size before and after.
We know we’re going to be working with the file system, so let’s add the System.IO namespace.
First, let’s demonstrate the use of the StreamWriter class. Obviously, there are many different ways to create a file, but StreamWriter is
one you should be familiar with. I will simply specify the name of the file here.
Now, let’s create a For loop to write “hello, world” 10,000 times. We’re going to call StreamWriter.Write to write it out.
Now, we just have to close the StreamWriter.
Reading it in is just as straightforward. We’re going to create a StreamReader instance, I’ll call it sr, and specify the filename. We can simply call
Console.WriteLine and specify the StreamReader.ReadToEnd method.
That just reads all of the data in from the file as one big string.
In practice, StreamWriter and StreamReader can handle binary data and buffer arrays, but it’s a little complex to actually read it. It’s easier to write
than it is to read, so you might end up using the BinaryWriter and BinaryReader classes instead.
So, once again, we need to close it once we’ve read in all the data.
Just for fun, so we can see how effective the compression is, I’m going to create a FileInfo instance and use it to display the size of the file,
both compressed and uncompressed.
FileInfo.Length is the actual size of the file in bytes.
One last call to Console.ReadKey to prevent the console from disappearing when we’re done. Now, I’ll run it.
That’s a lot of “Hello, worlds!” You can see that the entire file amounts to 129,987 bytes.
We’ll do that one more time. This time, we’re going to use the GZipStream class, which requires the System.IO.Compression
namespace. To write to a compressed stream, you need to create the GZipStream instance, and then use it when creating your StreamWriter
constructor. We’re going to specify File.Create to specify the filename, and I’ll use the same filename. And, you specify the compression
mode, which is either Compress or Decompress. Because we’re writing here, we’re going to be doing compression.
Now, in the StreamWriter, instead of specifying the file name, I’m going to specify the GZipStream instance that we just created. That is sufficient to
write the file—I just need to be sure to close the GZipStream instance.
I need to do the same thing to read the file—I create a GZipStream instance and specify that instance when I create the StreamReader.
Notice this time the CompressionMode is going to be Decompress. I’m reading it in—reading a compressed file and decompressing it.
Once again, I’m specifying the GZipStream instance instead of the filename in the StreamReader. I don’t need to change the code to
read or write within the StreamReader, because that’s abstracted.
That’s going to do it—I’ll run it and look at the bugs. I forgot a parenthesis. Happens to everybody! We’ll try this one more time.
Las ttime the file size was 129,987 bytes. This time it is 1,576 bytes. You can see that was a ridiculous compression—we got down to about
1.4% of the file size. You won’t normally see those kind of results, unfortunately, unless you’re compressing a text file that has the same pattern
repeated over and over. If you were to try to compress a picture file, you would get about 0% compression. Most data files will give you
20-30%. Your results will vary, but for text, it’s definitely worthwhile.