利用remoting实现文件传输

类别:.NET开发 点击:0 评论:0 推荐:

因为工作需要写了一个文件传输的东东

使用了System.Runtime.Remoting

客户端接口类:

Namespace Nail.Net.Remoting.Trans

    Public Interface iTransFile

        Function SendFile(ByVal FileName As String, ByVal bytes() As Byte, ByVal ClientCrc As Integer) As Integer
        Function CompareFile(ByVal FileName As String, ByVal ClientCrc As Integer) As Boolean
        Function GetFileDet(ByVal FileName As String) As Integer
        Function GetFileInfo(ByVal FileName As String) As Byte()
        Function GetFileInfo(ByVal FileName As String, ByVal Length As Integer) As Byte()
        Function GetFileInfo(ByVal FileName As String, ByVal Point As Integer, ByVal Length As Integer) As Byte()
        Property ServerTransSaveFilePath() As String
        Property ServerTempFilePath() As String

    End Interface

End Namespace

客户端类:(文件传输的实现在此)(包含1个窗体 后面窗体代码我会贴出来)(请添加引用System.Runtime.Remoting .net默认是不引用的 服务器端类同样)

Imports System.Runtime
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.IO
Imports System.Threading

Namespace Nail.Net.Remoting.Client

    Public Class FileTransClient
        Private TransTempSize As Integer
        Private TransType As TrType
        Private TransPort As Integer
        Private TransServerClassName As String
        Private TransServerIpAdr As String
        Private TransCRC32 As Integer
        Private Tsc As Nail.Net.Remoting.Trans.iTransFile
        Private Senconds As Integer = 10
        Private WithEvents Timer1 As New System.Windows.Forms.Timer
        Private NewTransForm As FrmTransfersUI
        Private TransFileName As String

        '文件全路径
        Public Property FullFileName() As String
            Get
                Return TransFileName
            End Get
            Set(ByVal Value As String)
                TransFileName = Value
            End Set
        End Property

        '缓存属性
        Public Property TempSize() As Integer
            Get
                Return TempSize
            End Get
            Set(ByVal Value As Integer)
                Me.TransTempSize = Value
            End Set
        End Property

        '端口属性
        Public Property Port() As Integer
            Get
                Return Port
            End Get
            Set(ByVal Value As Integer)
                Me.TransPort = Value
            End Set
        End Property

        '服务器端服务名属性
        Public Property ServerClassName() As String
            Get
                Return ServerClassName
            End Get
            Set(ByVal Value As String)
                Me.TransServerClassName = Value
            End Set
        End Property


        '服务器端IP地址属性
        Public Property ServerIpAdr() As String
            Get
                Return ServerIpAdr()
            End Get
            Set(ByVal Value As String)
                Me.TransServerIpAdr = Value
            End Set
        End Property

        '枚举传输类型
        Enum TrType
            TCP = 1
            HTTP = 2
        End Enum

        '传输类别属性
        Public Property Type() As TrType
            Get
                Return TransType
            End Get
            Set(ByVal Value As TrType)
                Me.TransType = Value
            End Set
        End Property

        '连接服务器
        Public Sub ConnetServer()
            Try
                Select Case TransType
                    Case TrType.TCP
                        Tsc = Activator.GetObject(GetType(Nail.Net.Remoting.Trans.iTransFile), "tcp://" & TransServerIpAdr & ":" & TransPort & "/" & TransServerClassName)
                    Case TrType.HTTP
                        Tsc = Activator.GetObject(GetType(Nail.Net.Remoting.Trans.iTransFile), "http://" & TransServerIpAdr & ":" & TransPort & "/" & TransServerClassName)
                End Select

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub

        Public Function CheckFileInfo(ByVal FileName As String) As Integer
            Dim crc As New Nail.FileTools.clsCrc32
            Return crc.CalcuateFile(FileName)
        End Function

        Public Sub SendFile()
            ShowUI()
            Timer1.Interval = 10
            Timer1.Start()
            Dim SendThead As New Thread(AddressOf SendFileToServer)
            SendThead.Start()
        End Sub

        '发送文件方法
        Private Sub SendFileToServer()
            Dim Crc As Integer = CheckFileInfo(TransFileName)
            Dim Fif As New FileInfo(TransFileName)
            Dim FileSm As New FileStream(TransFileName, FileMode.Open, FileAccess.Read, FileShare.Read)
            If FileSm.Length = 0 Then
                MsgBox("这是个空文件 请选择一个有效文件!")
                Exit Sub
            End If

            Dim bty(TransTempSize - 1) As Byte
            Dim Star As Integer = 0
            Dim i As Long

            NewTransForm.MaxOfProgress = FileSm.Length '进度条总量
            NewTransForm.FileSize = Fif.Length / 1000 & "Kbyte" '文件大小
            NewTransForm.TransfersFileName = Fif.Name '文件名
            Try
                If (Fif.Length / TransTempSize).ToString.IndexOf(".") > 0 Then
                    If (Fif.Length / TransTempSize).ToString.Substring(0, 1) = "0" Then
                        ReDim bty(Fif.Length - 1)
                        FileSm.Read(bty, 0, bty.Length)
                        Tsc.SendFile(Fif.Name, bty, Crc)
                        With NewTransForm
                            .RateOfProgress = bty.Length '进度条当前值
                            .PercentOfProgress = "100%"  '已完成的百分比
                            .LeaveTime = "完成"          '剩余时间
                            .Transed = bty.Length & "byte"  '已传输文件量
                            .RateOfTransfers = Math.Floor(bty.Length / Senconds) & "Kbyte/秒"  '传输速度
                        End With
                    Else
                        For i = Math.Floor(Fif.Length / TransTempSize) To 1 Step -1
                            FileSm.Read(bty, 0, bty.Length)
                            Tsc.SendFile(Fif.Name, bty, Crc)
                            With NewTransForm
                                .RateOfProgress = Star
                                .PercentOfProgress = Math.Floor(NewTransForm.pgbTransfers.Value / NewTransForm.pgbTransfers.Maximum * 100) & "%"
                                If Star > 0 Then
                                    .LeaveTime = TimeSpan.FromSeconds(Math.Floor((Fif.Length - Star) / 1000 / Math.Floor(Star / Senconds))).ToString
                                End If
                                .Transed = Star / 1000 & "Kbyte"
                                .RateOfTransfers = Math.Floor(Star / Senconds) & "Kbyte/秒"
                            End With
                            Star += TransTempSize
                            FileSm.Seek(Star, SeekOrigin.Begin)
                        Next
                        ReDim bty((Fif.Length - (Math.Floor(Fif.Length / TransTempSize)) * TransTempSize) - 1)
                        FileSm.Read(bty, 0, bty.Length)
                        Tsc.SendFile(Fif.Name, bty, Crc)
                        With NewTransForm
                            .RateOfProgress = .pgbTransfers.Maximum  '进度条当前值
                            .PercentOfProgress = "100%"
                            .LeaveTime = "完成"
                            .Transed = FileSm.Length / 1000 & "Kbyte"
                            .RateOfTransfers = Math.Floor(Star / Senconds) & "Kbyte/秒"  '传输速度
                        End With

                    End If

                Else

                    For i = Math.Floor(Fif.Length / TransTempSize) To 1 Step -1
                        FileSm.Read(bty, 0, bty.Length)
                        Tsc.SendFile(Fif.Name, bty, Crc)
                        With NewTransForm
                            .RateOfProgress = Star
                            .PercentOfProgress = Math.Floor(NewTransForm.pgbTransfers.Value / NewTransForm.pgbTransfers.Maximum * 100) & "%"
                            .Transed = Star / 1000 & "Kbyte"
                            If Star > 0 Then
                                .LeaveTime = TimeSpan.FromSeconds(Math.Floor((Fif.Length - Star) / 1000 / Math.Floor(Star / Senconds))).ToString
                            End If
                            .RateOfTransfers = Math.Floor(Star / Senconds) & "Kbyte/秒"  '传输速度
                        End With
                        Star += TransTempSize
                        FileSm.Seek(Star, SeekOrigin.Begin)

                    Next
                    NewTransForm.LeaveTime = "完成"
                   
                End If
                NewTransForm.btnCancle.Text = "确  定"
                Me.Timer1.Stop()
                Senconds = 10
                'NewTransForm.Close()
                'MsgBox("传输完成")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

            FileSm.Close()
        End Sub

        '接受文件方法
        Public Function GetFile(ByVal _filename As String) As Integer

        End Function

        '记时
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Senconds += 10
        End Sub

        Public Function ShowUI() As Integer
            NewTransForm = New FrmTransfersUI
            NewTransForm.Show()
            Return 0
        End Function
    End Class

End Namespace

客户端窗体(显示文件传输的进度 时间 大小 。。。。。)

Public Class FrmTransfersUI
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents pgbTransfers As System.Windows.Forms.ProgressBar
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents lblRateOfTransfers As System.Windows.Forms.Label
    Friend WithEvents lblTransfersFileName As System.Windows.Forms.Label
    Friend WithEvents btnCancle As System.Windows.Forms.Button
    Friend WithEvents lblFileSize As System.Windows.Forms.Label
    Friend WithEvents lblLeaveTime As System.Windows.Forms.Label
    Friend WithEvents lblPercentOfProgress As System.Windows.Forms.Label
    Friend WithEvents lblTransed As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.pgbTransfers = New System.Windows.Forms.ProgressBar
        Me.Button2 = New System.Windows.Forms.Button
        Me.lblRateOfTransfers = New System.Windows.Forms.Label
        Me.lblTransed = New System.Windows.Forms.Label
        Me.lblTransfersFileName = New System.Windows.Forms.Label
        Me.btnCancle = New System.Windows.Forms.Button
        Me.lblFileSize = New System.Windows.Forms.Label
        Me.lblLeaveTime = New System.Windows.Forms.Label
        Me.lblPercentOfProgress = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Button1.Enabled = False
        Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.Button1.Location = New System.Drawing.Point(0, 0)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(448, 136)
        Me.Button1.TabIndex = 0
        '
        'pgbTransfers
        '
        Me.pgbTransfers.Location = New System.Drawing.Point(10, 16)
        Me.pgbTransfers.Name = "pgbTransfers"
        Me.pgbTransfers.Size = New System.Drawing.Size(430, 12)
        Me.pgbTransfers.TabIndex = 1
        '
        'Button2
        '
        Me.Button2.Enabled = False
        Me.Button2.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.Button2.Location = New System.Drawing.Point(4, 4)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(442, 130)
        Me.Button2.TabIndex = 2
        '
        'lblRateOfTransfers
        '
        Me.lblRateOfTransfers.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.lblRateOfTransfers.Location = New System.Drawing.Point(10, 36)
        Me.lblRateOfTransfers.Name = "lblRateOfTransfers"
        Me.lblRateOfTransfers.Size = New System.Drawing.Size(220, 14)
        Me.lblRateOfTransfers.TabIndex = 3
        Me.lblRateOfTransfers.Text = "传输速度:"
        '
        'lblTransed
        '
        Me.lblTransed.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.lblTransed.Location = New System.Drawing.Point(10, 58)
        Me.lblTransed.Name = "lblTransed"
        Me.lblTransed.Size = New System.Drawing.Size(220, 14)
        Me.lblTransed.TabIndex = 4
        Me.lblTransed.Text = "已 传 送:"
        '
        'lblTransfersFileName
        '
        Me.lblTransfersFileName.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.lblTransfersFileName.Location = New System.Drawing.Point(10, 80)
        Me.lblTransfersFileName.Name = "lblTransfersFileName"
        Me.lblTransfersFileName.Size = New System.Drawing.Size(220, 14)
        Me.lblTransfersFileName.TabIndex = 5
        Me.lblTransfersFileName.Text = "传输文件:"
        '
        'btnCancle
        '
        Me.btnCancle.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.btnCancle.Location = New System.Drawing.Point(192, 104)
        Me.btnCancle.Name = "btnCancle"
        Me.btnCancle.TabIndex = 6
        Me.btnCancle.Text = "取  消"
        '
        'lblFileSize
        '
        Me.lblFileSize.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.lblFileSize.Location = New System.Drawing.Point(246, 80)
        Me.lblFileSize.Name = "lblFileSize"
        Me.lblFileSize.Size = New System.Drawing.Size(192, 14)
        Me.lblFileSize.TabIndex = 9
        Me.lblFileSize.Text = "文件大小:"
        '
        'lblLeaveTime
        '
        Me.lblLeaveTime.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.lblLeaveTime.Location = New System.Drawing.Point(246, 58)
        Me.lblLeaveTime.Name = "lblLeaveTime"
        Me.lblLeaveTime.Size = New System.Drawing.Size(192, 14)
        Me.lblLeaveTime.TabIndex = 8
        Me.lblLeaveTime.Text = "剩余时间:"
        '
        'lblPercentOfProgress
        '
        Me.lblPercentOfProgress.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.lblPercentOfProgress.Location = New System.Drawing.Point(246, 36)
        Me.lblPercentOfProgress.Name = "lblPercentOfProgress"
        Me.lblPercentOfProgress.Size = New System.Drawing.Size(192, 14)
        Me.lblPercentOfProgress.TabIndex = 7
        Me.lblPercentOfProgress.Text = "传输进度:"
        '
        'FrmTransfersUI
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(448, 136)
        Me.Controls.Add(Me.lblFileSize)
        Me.Controls.Add(Me.lblLeaveTime)
        Me.Controls.Add(Me.lblPercentOfProgress)
        Me.Controls.Add(Me.btnCancle)
        Me.Controls.Add(Me.lblTransfersFileName)
        Me.Controls.Add(Me.lblTransed)
        Me.Controls.Add(Me.lblRateOfTransfers)
        Me.Controls.Add(Me.pgbTransfers)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "FrmTransfersUI"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "FrmTransfersUI"
        Me.TopMost = True
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Event UserCancle()

    Public WriteOnly Property MaxOfProgress()
        Set(ByVal Value)
            Me.pgbTransfers.Maximum = Value
        End Set
    End Property

    Public WriteOnly Property RateOfProgress()
        Set(ByVal Value)
            Me.pgbTransfers.Value = Value
        End Set
    End Property

    Public WriteOnly Property RateOfTransfers() As String
        Set(ByVal Value As String)
            Me.lblRateOfTransfers.Text = "传输速度:" & Value
        End Set
    End Property

    Public WriteOnly Property Transed() As String
        Set(ByVal Value As String)
            Me.lblTransed.Text = "已 传 送:" & Value
        End Set
    End Property

    Public WriteOnly Property TransfersFileName() As String
        Set(ByVal Value As String)
            Me.lblTransfersFileName.Text = "传输文件:" & Value
        End Set
    End Property

    Public WriteOnly Property PercentOfProgress() As String
        Set(ByVal Value As String)
            Me.lblPercentOfProgress.Text = "传输进度:" & Value
        End Set
    End Property

    Public WriteOnly Property LeaveTime() As String
        Set(ByVal Value As String)
            Me.lblLeaveTime.Text = "剩余时间:" & Value
        End Set
    End Property

    Public WriteOnly Property FileSize() As String
        Set(ByVal Value As String)
            Me.lblFileSize.Text = "文件大小:" & Value
        End Set
    End Property


    Private Sub btnCancle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancle.Click

        If Me.btnCancle.Text = "确  定" Then
            Me.Close()
        Else
            RaiseEvent UserCancle()
        End If


    End Sub
End Class

服务器端类   下面还有个模块

Imports System.Runtime
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.IO

Namespace Nail.Net.Remoting.Server
    Public Class FileTransServer

        Private TransServerClassName As String
        Private TransType As TrType
        Private TransPort As Integer
        Private TransCRC32 As Integer

        Enum TrType
            TCP = 1
            HTTP = 2
        End Enum

        Public Property Type() As TrType
            Get
                Return TransType
            End Get
            Set(ByVal Value As TrType)
                Me.TransType = Value
            End Set
        End Property

        Public Property ServerTransSaveFilePath() As String
            Get
                Return TransSaveFilePath
            End Get
            Set(ByVal Value As String)
                TransSaveFilePath = Value
            End Set
        End Property

        Public Property ServerTempFilePath() As String
            Get
                Return TempFilePath
            End Get
            Set(ByVal Value As String)
                TempFilePath = Value
            End Set
        End Property

        Public Property ServerClassName() As String
            Get
                Return ServerClassName
            End Get
            Set(ByVal Value As String)
                Me.TransServerClassName = Value
            End Set
        End Property

        Public Property Port() As Integer
            Get
                Return Port
            End Get
            Set(ByVal Value As Integer)
                Me.TransPort = Value
            End Set
        End Property

        Public Sub StarServer()
            Select Case TransType
                Case TrType.TCP
                    Dim a As New System.Runtime.Remoting.Channels.Tcp.TcpChannel(TransPort)
                    ChannelServices.RegisterChannel(a)
                Case TrType.HTTP
                    Dim a As New System.Runtime.Remoting.Channels.Http.HttpChannel(TransPort)
                    ChannelServices.RegisterChannel(a)
            End Select
            System.Runtime.Remoting.RemotingConfiguration.ApplicationName = TransServerClassName
            RemotingConfiguration.RegisterWellKnownServiceType(GetType(TransFile), TransServerClassName, WellKnownObjectMode.SingleCall)

        End Sub

    End Class

    Public Class TransFile
        Inherits MarshalByRefObject
        Implements Nail.Net.Remoting.Trans.iTransFile

        Public Function CompareFile(ByVal FileName As String, ByVal ClientCrc As Integer) As Boolean Implements Nail.Net.Remoting.Trans.iTransFile.CompareFile
            Dim crc As New Nail.FileTools.clsCrc32
            If crc.CalcuateFile(FileName) = ClientCrc Then
                Return True
            Else
                Return False
            End If
        End Function

        Public Function GetFileDet(ByVal FileName As String) As Integer Implements Nail.Net.Remoting.Trans.iTransFile.GetFileDet
            Dim c As New FileInfo(FileName)
            Return c.Length
        End Function

        Public Function GetFileInfo(ByVal FileName As String) As Byte() Implements Nail.Net.Remoting.Trans.iTransFile.GetFileInfo
            Dim FullName As String = FileName
            Dim c As New FileInfo(FullName)
            Return GetFileInfo(FileName, 0, c.Length)
        End Function

        Public Function GetFileInfo(ByVal FileName As String, ByVal Length As Integer) As Byte() Implements Nail.Net.Remoting.Trans.iTransFile.GetFileInfo
            Return GetFileInfo(FileName, 0, Length)
        End Function

        Public Function GetFileInfo(ByVal FileName As String, ByVal Point As Integer, ByVal Length As Integer) As Byte() Implements Nail.Net.Remoting.Trans.iTransFile.GetFileInfo
            Dim FullName As String = FileName
            Dim c As New FileInfo(FullName)
            Dim x As New FileStream(FullName, FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim bty(Length - 1) As Byte
            x.Seek(Point, SeekOrigin.Begin)
            x.Read(bty, 0, Length)
            Return bty
        End Function

        Public Function SendFile(ByVal FileName As String, ByVal bytes() As Byte, ByVal ClientCrc As Integer) As Integer Implements Nail.Net.Remoting.Trans.iTransFile.SendFile
            Try
                Dim a As New FileStream(TempFilePath & FileName, FileMode.OpenOrCreate)
                a.Seek(a.Length, SeekOrigin.Begin)
                a.Write(bytes, 0, bytes.Length)
                a.Close()
                Return 0
            Catch ex As Exception
                MsgBox(ex.ToString)
                Return 1
            Finally
                'Dim Finf As New FileInfo(TempFilePath & FileName)
                'If CompareFile(TempFilePath & FileName, ClientCrc) = True Then
                '    '拷贝临时文件()
                '    File.Copy(TempFilePath & FileName, TransSaveFilePath & FileName, True)
                'Else
                '    '删除临时文件()
                '    Finf.Delete()
                'End If
            End Try
           
        End Function

        Public Property ServerTempFilePath() As String Implements Nail.Net.Remoting.Trans.iTransFile.ServerTempFilePath
            Get
                Return TempFilePath
            End Get
            Set(ByVal Value As String)
                TempFilePath = Value
            End Set
        End Property

        Public Property ServerTransSaveFilePath() As String Implements Nail.Net.Remoting.Trans.iTransFile.ServerTransSaveFilePath
            Get
                Return TransSaveFilePath
            End Get
            Set(ByVal Value As String)
                TransSaveFilePath = Value
            End Set
        End Property
    End Class

End Namespace

模块:

Module Server
    Public TransSaveFilePath As String
    Public TempFilePath As String
End Module

使用方法:

服务器端

示例:

Dim Server As New Nail.Net.Remoting.Server.FileTransServer
        Server.ServerClassName = "aaa"
        Server.Port = 10001
        Server.Type = Nail.Net.Remoting.Server.FileTransServer.TrType.HTTP
        Server.ServerTempFilePath = "f:\"
        Server.ServerTransSaveFilePath = "e:\"
        Server.StarServer()

客户端

 cl.Port = 10001
        cl.ServerClassName = "aaa"
        cl.TempSize = 102400
        cl.ServerIpAdr = "localhost"
        cl.FullFileName = "c:\111.rar"
        cl.Type = Nail.Net.Remoting.Client.FileTransClient.TrType.HTTP
        cl.ConnetServer()

发送文件:

 cl.SendFile()

最后忘记贴文件校对了 照书上搞的CRC32

Imports System.IO

Namespace Nail.FileTools

    Public Class clsCrc32
        Private Const TABLESIZE As Integer = 256
        Private Const DEFAULTPOLYNOMIAL As Integer = &HEDB88320
        Private Const DEFAULTIALVALUE As Integer = &HFFFFFFFF
        Private lookup(TABLESIZE - 1) As Integer
        Private crcPolynomial As Integer = 0

        Public Sub New()
            Me.New(DEFAULTPOLYNOMIAL)
        End Sub

        Public Sub New(ByVal crcPolynomial As Integer)
            Me.crcPolynomial = crcPolynomial
            InitLookupTable()
        End Sub

        Public Property Polynomial() As Integer
            Get
                Return crcPolynomial
            End Get
            Set(ByVal Value As Integer)
                Me.crcPolynomial = Value
                InitLookupTable()
            End Set
        End Property

        Public Overloads Function CalculateBlock(ByVal bytes() As Byte) As Integer
            Return CalculateBlock(bytes, 0, bytes.Length)
        End Function

        Public Overloads Function CalculateBlock(ByVal bytes() As Byte, ByVal index As Integer, ByVal length As Integer) As Integer
            Return CalculateBlock(bytes, index, length, DEFAULTIALVALUE)
        End Function

        Public Overloads Function CalculateBlock(ByVal bytes() As Byte, ByVal index As Integer, ByVal length As Integer, ByVal initialValue As Integer) As Integer
            If bytes Is Nothing Then

            ElseIf index < 0 Or length <= 0 Or index + length > bytes.Length Then

            End If
            Return Not internalcalculateBlock(bytes, index, length, initialValue)
        End Function

        Private Function internalcalculateBlock(ByVal bytes() As Byte, ByVal index As Integer, ByVal length As Integer, ByVal initialValue As Integer) As Integer
            Dim crc As Integer = initialValue
            Dim shiftCrc As Integer

            Dim position As Integer
            For position = index To length - 1
                shiftCrc = crc And &HFFFFFF00
                shiftCrc = shiftCrc / &H100
                shiftCrc = shiftCrc And &HFFFFFF
                crc = shiftCrc Xor lookup(bytes(position)) Xor (crc And &HFF)
            Next
            Return crc
        End Function

        Public Overloads Function CalcuateFile(ByVal path As String) As Integer
            Return CalcuateFile(path, DEFAULTIALVALUE)

        End Function

        Public Overloads Function CalcuateFile(ByVal path As String, ByVal initialValue As Integer) As Integer
            If path Is Nothing Then

            ElseIf path.Length = 0 Then

            End If

            Return Not InternalCalculateFile(path, initialValue)
        End Function

        Private Function InternalCalculateFile(ByVal path As String, ByVal initialValue As Integer) As Integer

            Const blockSize As Integer = 4096
            Dim count As Integer
            Dim inStream As System.IO.FileStream
            Dim bytes(blockSize - 1) As Byte
            Dim crc As Integer = initialValue
            Try
                inStream = System.IO.File.Open(path, FileMode.Open, FileAccess.Read)
                While inStream.Position < inStream.Length
                    count = inStream.Read(bytes, 0, blockSize)
                    crc = internalcalculateBlock(bytes, 0, count, crc)
                End While
            Finally
                If Not inStream Is Nothing Then
                    inStream.Close()
                End If
            End Try
            Return crc
        End Function

        Private Sub InitLookupTable()
            Dim bytecount, bitcount As Integer
            Dim crc, shiftCrc As Integer
            For bytecount = 0 To TABLESIZE - 1
                crc = bytecount
                For bitcount = 0 To 7
                    shiftCrc = crc And &HFFFFFFFE
                    shiftCrc = shiftCrc \ &H2
                    shiftCrc = shiftCrc And &H7FFFFFFF
                    If (crc And &H1) Then
                        crc = shiftCrc Xor crcPolynomial
                    Else
                        crc = shiftCrc
                    End If
                Next
                lookup(bytecount) = crc
            Next
        End Sub

    End Class

End Namespace

本文地址:http://com.8s8s.com/it/it41115.htm