How to read all stream bytes, but the last 8

I have the following code:

using (var fs = new FileStream(@"C:\dump.bin", FileMode.Create))
{
    income.CopyTo(fs);
}

income- this is the stream that I need to save to disk, the problem is that I want to ignore the last 8 bytes and save everything before that. The revenue stream is read-only, forward only, so I cannot predict its size, and I do not want to load the entire stream into memory due to the sending of huge files.

Any help would be appreciated.

+4
source share
1 answer

Perhaps (or rather, probably) there is a cleaner way to do this, but being pragmatic at that moment, the first thought that comes to my mind is this:

using (var fs = new FileStream(@"C:\dump.bin", FileMode.Create))
{
    income.CopyTo(fs);
    fs.SetLength(Math.Max(income.Length - 8, 0));
}

Which sets the length of the file after it is written.

+1

Source: https://habr.com/ru/post/1649737/


All Articles