Crc32.NET icon indicating copy to clipboard operation
Crc32.NET copied to clipboard

Support for stream

Open angedelamort opened this issue 7 years ago • 4 comments

I was wondering if you could add support for FileStream (or Stream in general)?

Since we usually use files for computing the CRC, I find it annoying to load the file in memory in order to get all the bytes. When you have big files, it's not really good practice. And I saw that most of libraries use the same interface as yours.

So, that's what I can do right now:

var bytes = File.ReadAllBytes(path);
var crc32 = Crc32Algorithm.Compute(bytes);

What would be nice:

using (var stream = new FileStream(path, FileMode.Open))
{
    var crc32 = Crc32Algorithm.Compute(stream);
}

Thanks

angedelamort avatar Jan 21 '19 02:01 angedelamort

I happened to need/want this too, so see #14 if you want to try out my proposed implementation

InvisiblePhil avatar Feb 05 '19 17:02 InvisiblePhil

Will check it later. Thank you.

force-net avatar Feb 05 '19 17:02 force-net

you can

using (var stream = new FileStream(path, FileMode.Open))
using (var crc = new Crc32Algorithm())
{
    var crc32bytes = crc.Compute(stream);
    var crc32 = BitConverter.ToUInt32(crc32bytes, 0);
}

kgamecarter avatar Jun 21 '19 10:06 kgamecarter

Doh, didn't realise the base class did that. Looking at the source it's very similar to what I hacked together (right down to the 4kb buffer size). Closing my PR as redundant.

InvisiblePhil avatar Jun 21 '19 10:06 InvisiblePhil