⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 This program is used to calculating CRC32 checksums of files in order to verify the integrity of fil
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace sfv
{
    class Program
    {
        static byte oldValue = 0;

        static bool OnProgress(byte percent)
        {
            if (oldValue != percent)
            {
                oldValue = percent;

                Console_MoveX(-3);
                
                Console.Write(percent.ToString().PadLeft(3, '0'));
            }

            return true;
        }

        static StreamWriter text = null;

        static void Console_OnCancel(object sender, ConsoleCancelEventArgs e)
        {
            if (text != null)
                text.Close();
        }

        static void Console_MoveX(int value)
        {
            while (value != 0)
            {
                try
                {
                    Console.CursorLeft += value;

                    value = 0;
                }
                catch
                {
                    try
                    {
                        if (value > 0)
                        {                            
                            value -= Console.WindowWidth;
                            Console.CursorTop++;
                        }
                        else
                        {
                            value += Console.WindowWidth;
                            Console.CursorTop--;
                        }

                        continue;
                    }
                    catch
                    {
                        return;
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            try
            {
                if (args.Length >= 3)
                {
                    Console.TreatControlCAsInput = false;

                    Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_OnCancel);

                    string[] files = Directory.GetFiles(args[0], args[1], SearchOption.AllDirectories);

                    text = new StreamWriter(Path.ChangeExtension(args[2], ".sfv"), false, Encoding.Default);                    

                    try
                    {
                        foreach (string file in files)
                        {
                            Console.Write(Path.GetFileName(file) + " : 000");

                            string crc = "00000000";

                            try
                            {
                                crc = CRC32.GetCrcOfFile(file, OnProgress).ToString("X8");

                                Console_MoveX(-3);
                                Console.WriteLine(crc);
                            }
                            catch (Exception ex)
                            {
                                Console_MoveX(-3);
                                Console.WriteLine("   ");
                                Console.WriteLine(ex.Message);
                            }

                            text.WriteLine(file + " " + crc);
                        }
                    }
                    finally
                    {
                        text.Close();
                    }

                    throw new Exception("Press any key to continue.");                  
                }
                else
                    throw new Exception("Usage : sfv [folder] [mask] [output file]");
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine(ex.Message);
            }

            //Console.ReadKey();
        }
    }

    public class CRC32
    {
        public delegate bool ProgressEvent(byte percent);

        uint[] table;

        public CRC32()
        {
            uint poly = 0xedb88320;
            table = new uint[256];
            uint temp = 0;
            for (uint i = 0; i < table.Length; i++)
            {
                temp = i;
                for (int j = 8; j > 0; j--)
                {
                    if ((temp & 1) == 1)
                        temp = (uint)((temp >> 1) ^ poly);
                    else
                        temp >>= 1;
                }
                table[i] = temp;
            }
        }

        public uint Init()
        {
            return 0xffffffff;
        }

        public uint Next(uint crc, byte[] bytes, int offset, int size)
        {
            for (int i = offset; i < offset + size; i++)
                crc = (uint)((crc >> 8) ^ table[(byte)((crc ^ bytes[i]) & 0xff)]);
            return crc;
        }

        public uint Final(uint crc)
        {
            return ~crc;
        }

        public static uint GetCrcOfFile(string Filename, ProgressEvent progress)
        {
            uint result = 0;

            FileStream file = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read);

            try
            {
                CRC32 crc32 = new CRC32();

                uint crc = crc32.Init();

                byte percent = 0;

                while (file.Position < file.Length & progress(percent))
                {
                    byte[] buff = new byte[64 * 1024];

                    int bytesReaded = file.Read(buff, 0, buff.Length);

                    crc = crc32.Next(crc, buff, 0, bytesReaded);

                    percent = Convert.ToByte(
                        Math.Round((Convert.ToDouble(file.Position) / Convert.ToDouble(file.Length)) * 100));
                }

                result = crc32.Final(crc);

            }
            finally
            {
                file.Close();
            }

            return result;
        }
    } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -