In this project Iím going to use StreamWriter and StreamReader to write data and read it
from a file, and then Iím going to do the same thing using the GZipStream class to compress
the data. Weíll compare the file sizes.
We know weíre going to need the System.IO namespace, because thatís required by StreamWriter
and StreamReader. First, Iíll create the StreamWriter instance and specify the filenameóIíll
call it data.bin. Now, writing to this file is really easyówe just need to call StreamWriter.Write.
Iím going to make the file fairly big, so Iím going to create a For loop to write ìhello,
worldî 10,000 times to it. Notice that StreamWriter.Write can take a variety of different types. Iím
just writing a string to it, but you can also write integers, or Boolean values, or character
arrays.
We open it up, so we need to close it at the end. The file has been created at this point,
and we just need to create a StreamReader to read it in. Creating the StreamReader is
done in exactly the same way as creating the StreamWriter wasówe just need to specify
the filename.
Now, Iím going to display the entire file to the console by calling StreamReader.ReadToEnd.
In practice, you probably wouldnít read the entire file in this wayóyouíd probably write
some binary data to it and then read it back selectively, as you would a binary file.
One last thing I want to do, just for fun, is to check the file size so we can see how
effective the compression is when weíre doing. Iíll do this by creating a FileInfo instance
and specifying the filename. Now, Iíll write to the console
the size of the file. Length is just an integer that is the size of the file in bytes.
One last thing, weíll do a Console.ReadKey to pause the console at the end.
The total size of the file is about 140k.
Now Iím going to go back and update this to use the GZipStream class instead of using
StreamWriter to write directly to the file. I need to Import the System.IO.Compression
namespace to get GZipStream.
I need to, for the first GZipStream parameter, provide a stream. One easy way to do that
is to call File.Create. As the second parameter, I need to specify either CompressionMode.Compress
or Decompress. Iím writing to the file, so I want to compress it to make it smaller.
When I read it, I will specify Decompress. When I create the StreamWriter, this is an
overloaded constructor, so instead of specifying the filename I will specify my instance of
GZipStream.
Ultimately it writes to the same file, but it goes through that extra compression layer
provided by the GZipStream class. I donít need to change the writing at all, I just
need to close the GZipStream instance.
When I read it, I need to do exactly the same thingóin fact, Iíll just copy
the GZipStream constructor. Instead of calling File.Create, Iíll call File.OpenRead. The
filename is, of course, the same. Instead of compressing it, Iíll decompress it.
Once again, the reading code does not need to change, I just need to close my instance
of GZipStream.
Whoops, I almost forgot to replace my StreamReader constructor to use my new instance of GZipStream.
So, this is ready to run. This time, the file size is 1.7k aboutóweíre down to about 1%
of the original file size
In this case, the compression was extremely effective, but itís not really a real-world
scenario. In the real world, you wonít simply be repeating the same phrase over and over
again, which would make the compression extremely effective. In the real world, you will see
a high-level of compression for text. Probably not 99%, but maybe 90%. For a binary file,
you might see 30%-60% compression. If you were compressing a JPEG or MP3 which are already
compressed, you wonít see any compression. In fact, the compression might actually make
the file size a little bit bigger.