Example #1
0
        public static uint GetFileCRC32(string path, ref long byteCount, BackgroundWorker bw)
        {
            if (String.IsNullOrEmpty(path))
                throw new ArgumentNullException("path");

            Crc32 crc32 = new Crc32();

            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                byte[] buffer = new byte[4096];

                int bytesRead = 0;
                long size = fs.Length;
                long totalBytesRead = 0;
                int ticks = 0;

                while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                {
                    ticks++;
                    totalBytesRead += bytesRead;
                    byteCount += bytesRead;

                    crc32.Update(buffer, 0, bytesRead);

                    if (bw != null && ticks == 10) // Only report once every 10 loops so we dont hose the CPU
                    {
                        bw.ReportProgress((int)((double)totalBytesRead * 100 / size));
                        ticks = 0;
                    }
                }
            }

            return crc32.Value;
        }
Example #2
0
        public static uint GetStreamCRC32(Stream stream)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");

            if (!stream.CanRead)
                throw new ArgumentException("stream is not readable.");

            Crc32 crc32 = new Crc32();

            byte[] buffer = new byte[4096];
            int bytesRead = 0;

            stream.Position = 0;
            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
            {
                crc32.Update(buffer, 0, bytesRead);
            }
            stream.Position = 0;

            return crc32.Value;
        }