I'm wondering if there is a better/inbuilt way, other than using a byte buffer and looping, to read from one stream and write it to another (in .NET). Generally this is done to apply a transform to a stream and move it on.
In this instance, what I am loading a file, putting it through a deflate stream and writing it out to a file (Error handling removed for simplicity):
byte[] buffer = new byte[10000000];
using (FileStream fsin = new FileStream(filename, FileMode.Open))
{
using (FileStream fsout = new FileStream(zipfilename, FileMode.CreateNew))
{
using (DeflateStream ds = new DeflateStream(fsout, CompressionMode.Compress))
{
int read = 0;
do
{
read = fsin.Read(buffer, 0, buffer.Length);
ds.Write(buffer, 0, read);
}
while (read > 0);
}
}
}
buffer = null;
Edit:
.NET 4.0 now has a Stream.CopyTo function, Hallelujah