Since FileStream.CopyTo is a .NET 4.0 feature, the above code is it's replacement in the .NET 2.0, .NET 3.0, .NET 3.5
private static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read <= 0) return;
output.Write (buffer, 0, read);
}
}
To use it, just copy the above function in your code together with the example provided from MSDN and then replace the input.CopyTo(output) lines with the above function, so it will look like this:
CopyStream(input, output);
NOTE: The real power of GZip is in the files of any kind expect .jpg pictures. The .jpg picture compression rate is very bad, because it actually raises the compressed file size and not lowers it. Or maybe it's just the .NET 2.0 version of GZip.
But for all other file types, the compressed size is reduced to almost 50% of the original uncompressed file.
---
This is the .NET 4 version of the documentation, so it's expected that the revisit samples to take advantage of new APIs available. To obtain the sample available for .NET 3.5, go to http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream(VS.90).aspx.
Maira Wenzel
CLR Documentation Manager