Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

ZIP CRC32 Static Method and Values

, 30 Jun 2008
A static C# method and values for a small footprint paste into your C# code
using System;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
[assembly: AssemblyCompany("Appso Associates")]
[assembly: AssemblyCopyright("Copyright � Appso Associates 2005")]
[assembly: AssemblyProduct("Appso.TestBench(Testing Crc32)")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Appso
{
    static class TestBench
    {
        //*****************************************************************************

        #region Testing Crc32 ProcessFile() THE INTERESTING PART OF THE TEST
        static void ProcessFile(FileInfo theFile)
        {
            Display.Append(theFile.Name);
            Display.AppendTab(theFile.Length.ToString());
            try
            {
                // compute Crc for various buffer sizes 0 to Totals.Length
                uint[] iCrc = new uint[Totals.Length];
                long[] iTotalTicks = new long[Totals.Length];
                for (int iTotal = 0; iTotal < Totals.Length; iTotal++)
                {
                    byte[] xBuf = new byte[Totals[iTotal]];
                    Stream xStream = theFile.OpenRead();
                    
                    int iRead;
                    long iStartTicks = System.DateTime.Now.Ticks; // START TICKING
                    while ( Display.IsAvailable 
                        && 0 < (iRead = xStream.Read(xBuf, 0, xBuf.Length)))
                    {
                        iCrc[iTotal] 
                            = YourNameSpace.YourClass.Crc32(iCrc[iTotal], xBuf, 0, iRead);
                        Application.DoEvents(); // because we are in the main thread
                    }
                    iTotalTicks[iTotal] 
                        = System.DateTime.Now.Ticks - iStartTicks; // STOP TICKING
                }
                
                // Having successfully processed all variations,
                // we post the totals and print the results for the file
                TotalBytes += theFile.Length;
                for (int iTotal = 0; iTotal < iTotalTicks.Length; iTotal++)
                {
                    TotalTicks[iTotal] += iTotalTicks[iTotal];
                    if (iTotal < 1)
                        Display.AppendTab(iCrc[iTotal].ToString("x8"));
                    else
                        if (iCrc[iTotal] != iCrc[0])
                            Display.AppendTab(iCrc[iTotal].ToString("x8") 
                                + "(" + iTotal.ToString() + ") WHOOPS");
                }
                Display.Append("");
            }
            catch
            {
                Display.AppendTab("NOT PROCESSED");
            }
            Display.AppendLine("");
        }
        #endregion

        //*****************************************************************************

        #region Testing Crc32 SUPPORTNG METHODS/PROPERTIES
        static void Test_Run(string theCommandLine)
        {
            Display.Clear();
            DirectoryInfo xDir = new DirectoryInfo(theCommandLine);

            Display.AppendLine("Directory: " + xDir.FullName);
            Display.AppendLine("Showing tabbed: filename, size, Crc(hex), and errors detected (if any)");
            Display.AppendLine("");

            TotalBytes = 0; TotalTicks = new long[Totals.Length];
            FileInfo[] xFiles = xDir.GetFiles();

            int iFile = 0;
            while (iFile < xFiles.Length && Display.Room > 4000)
                ProcessFile(xFiles[iFile++]);

            Display.AppendLine("");
            Display.AppendLine("TotalBytes: " + TotalBytes.ToString());
            for (int iTotal = 0; iTotal < Totals.Length; iTotal++)
                Display.AppendLine
                    ( "Buffer: " + Totals[iTotal].ToString() + "   "
                    + "\tTicks: " + TotalTicks[iTotal].ToString()
                    + "\tPerByte: " + ((double)TotalTicks[iTotal] / TotalBytes).ToString("0.00") 
                    );
            Display.AppendLine("");
            Display.AppendLine("NOTE: paste above tabbed results into EXCEL sheet to sort/review.");
            Display.AppendLine("Compare results with a commercial product such as WinZip.");
            DefaultCommand = xDir;
        }
        // SPECIFY THE VARIOUS BUFFER SIZES HERE (0=none, last one for explicit processing)
        static long[] Totals = { 64, 128, 1024, 4096, 8192 };
        static long TotalBytes = 0;
        static long[] TotalTicks;
        static DirectoryInfo DefaultCommand = new DirectoryInfo(System.Environment.CurrentDirectory);
        const string Test_Title = "Testing Crc32";
        static void Test_Entry()
        {
            Display.AppendLine(Break);
            Display.AppendLine("Enter/change the last line to a directory path and then press " + RunKey + ".");
            Display.AppendLine(DefaultCommand.ToString());
        }
        #endregion

        //*****************************************************************************

        #region Appso.TestBench ENTRY/DISPLAY STUFF ... NOTHING TO SEE HERE
        [STAThread]
        static void Main() { Application.Run(Display = new DisplayForm()); }
        static DisplayForm Display; static readonly string Break = new string('*', 80);
        const char CtlR_DC2 = (char)18; const string RunKey = "Ctrl-R";
        const string zs_Waiting_for_entry = "  Waiting for last line entry then " + RunKey + ".";
        const string zs_PROCESSING = "  PROCESSING";
        class DisplayForm : Form
        {
            TextBox zxResults; string zsTitlePrefix = "";
            internal DisplayForm()
            {
                Visible = false; SuspendLayout(); FormBorderStyle = FormBorderStyle.SizableToolWindow; StartPosition = FormStartPosition.Manual;
                Bounds = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea; Width = (Width * 2)/3 ; KeyPreview = true;
                zsTitlePrefix = "Appso.TestBench(" + Test_Title + ")      "; SetTitle(zs_Waiting_for_entry);
                zxResults = new TextBox(); zxResults.Multiline = true; zxResults.Dock = DockStyle.Fill;
                zxResults.ScrollBars = ScrollBars.Both; zxResults.WordWrap = true; 
                zxResults.Font = new System.Drawing.Font
                    (System.Drawing.FontFamily.GenericMonospace, 12
                    , System.Drawing.FontStyle.Bold);
                Controls.Add(zxResults); ResumeLayout();
            }
            internal void SetTitle(string theText) { Text = zsTitlePrefix + theText; }
            internal void Append(string theText) 
            {
                Application.DoEvents();
                if (Room < theText.Length) return; // ignore stuff that will fail
                zxResults.SuspendLayout(); zxResults.AppendText(theText); zxResults.ResumeLayout(); 
                Application.DoEvents();
            }
            internal void AppendTab(string thenTheText) { Append("\t" + thenTheText); }
            internal void AppendLine(string theText){ Append(theText + Environment.NewLine); }
            internal int Room { get { return zxResults.MaxLength - zxResults.Text.Length; } }
            internal void Clear() { zxResults.SuspendLayout(); zxResults.Text = ""; zxResults.ResumeLayout(); }
            protected override void OnLoad(EventArgs e) { base.OnLoad(e); Test_Entry(); }
            internal bool IsAvailable { get { return !zxResults.IsDisposed; } }
            protected override void OnKeyPress(KeyPressEventArgs e)
            {
                if (e.KeyChar != CtlR_DC2) base.OnKeyPress(e);
                else
                {
                    string sCmd = ""; int i = zxResults.Lines.Length; while (--i >= 0) if (0 < (sCmd = zxResults.Lines[i].Trim()).Length) break;
                    try { SetTitle(zs_PROCESSING); Test_Run(sCmd); }
                    catch (Exception err)
                    {
                        if (zxResults.IsDisposed) return; // form closed before processing done
                        Display.AppendLine(Break); Display.AppendLine("PROCESSING ERROR:"); Display.AppendLine(err.Message);
                    }
                    SetTitle(zs_Waiting_for_entry);
                    Test_Entry();
                }
            }
        }
        #endregion

        //*****************************************************************************
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

G.Franklin

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 30 Jun 2008
Article Copyright 2007 by G.Franklin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid