FTP 下载大于 2GB 的文件 (VB.net)

FTP download files larger than 2GB (VB.net)

缺少最后几个字节,文件已损坏 - 悬赏

我现在添加了赏金来解决这个问题。我将整数类型更改为 int64,这似乎已经解决了部分问题,但现在当它完成下载时,它有时会错过最后 1-5 个字节,这在 return 中损坏了文件,所以它不能'不要解压缩。是否有另一种关闭流的方法,以确保文件已完全下载并避免损坏?我已经尝试过这个简单的代码,但同样的问题发生了。

Imports System.ComponentModel
Imports System.Net

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
    Dim WithEvents WC As New WebClient
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WC.DownloadFileAsync(New Uri("ftp://dmr-ftp-user:dmrpassword@5.44.137.84/ESStatistikListeModtag/ESStatistikListeModtag-20160327-094743.zip"), "C:\XML\ESStatistikListeModtag-20160327-094743.zip.zip")
    End Sub
    Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
        If e.ProgressPercentage = 100 Then
            MsgBox("File download - 100%") 'This message box does trigger once the download is complete, but file is still corrupted.
        End If
    End Sub

    Private Sub WC_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles WC.DownloadFileCompleted
        MsgBox("Complete") ' This message box doesn't trigger!
    End Sub
End Class

老问题:

我正在尝试使用我的 vb.net 应用程序从 FTP 服务器下载一个 zip 文件。我当前的源代码在下面 posted。这适用于较小的文件,但当我超过 2GB 的限制时,出现以下异常:

"Arithmetic operation resulted in an overflow"

这是一个大小约为 2.5 GB 的文件,每个文件都略有增加(大约 20 MB),所以我需要一个可以处理大文件的解决方案,希望没有限制。最后我也想用程序解压缩文件,所以如果你有任何想法,你也可以 post 这个。谢谢!

 Private Sub Download(ByVal filePath As String, ByVal fileName As String)
        FTPSettings.IP = "0.0.0.0"
        FTPSettings.UserID = "ftp-user"
        FTPSettings.Password = "ftp-pass"
        Dim reqFTP As FtpWebRequest = Nothing
        Dim ftpStream As Stream = Nothing
        Try
            Dim outputStream As New FileStream(filePath + "\" + fileName, FileMode.Create)
            reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + fileName)), FtpWebRequest)
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
            reqFTP.UseBinary = True
            reqFTP.Credentials = New NetworkCredential(FTPSettings.UserID, FTPSettings.Password)
            Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
            ftpStream = response.GetResponseStream()
            Dim cl As Long = response.ContentLength
            Dim bufferSize As Integer = 2048
            Dim readCount As Int64
            Dim buffer As Byte() = New Byte(bufferSize - 1) {}
            Dim size As Int64

            readCount = ftpStream.Read(buffer, 0, bufferSize)
            While readCount > 0
                outputStream.Write(buffer, 0, readCount)
                readCount = ftpStream.Read(buffer, 0, bufferSize)

                If readCount = bufferSize Then
                    size += readCount
                    Label1.Text = size
                    Label1.Refresh()
                End If

              End While

            ftpStream.Close()
            outputStream.Close()
            response.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
            If ftpStream IsNot Nothing Then
                ftpStream.Close()
                ftpStream.Dispose()
            End If
            Throw New Exception(ex.Message.ToString())
        End Try
    End Sub
    Public NotInheritable Class FTPSettings
        Private Sub New()
        End Sub
        Public Shared Property IP() As String
            Get
                Return m_IP
            End Get
            Set(ByVal value As String)
                m_IP = value
            End Set
        End Property
        Private Shared m_IP As String
        Public Shared Property UserID() As String
            Get
                Return m_UserID
            End Get
            Set(ByVal value As String)
                m_UserID = value
            End Set
        End Property
        Private Shared m_UserID As String
        Public Shared Property Password() As String
            Get
                Return m_Password
            End Get
            Set(ByVal value As String)
                m_Password = value
            End Set
        End Property
        Private Shared m_Password As String
    End Class
End Class

我以前在使用 WebClient 时遇到过类似的问题,特别是在将它与 WithEvents 语句一起使用时。 看看像这样重写代码是否能解决问题:

Imports System.ComponentModel
Imports System.Net

Public Class Form1
    Private wc As New WebClient()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        wc = New WebClient()

        AddHandler wc.DownloadProgressChanged, Sub(s As Object, ByVal e As DownloadProgressChangedEventArgs)
                                                Me.Invoke(New MethodInvoker(Sub() ProgressBar1.Value = e.ProgressPercentage))
                                            End Sub

        AddHandler wc.DownloadFileCompleted, Sub(s As Object, e As ComponentModel.AsyncCompletedEventArgs)
                                                MsgBox("Complete")
                                            End Sub
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        wc.DownloadFileAsync(New Uri("ftp://dmr-ftp-user:dmrpassword@5.44.137.84/ESStatistikListeModtag/ESStatistikListeModtag-20160327-094743.zip"), "C:\XML\ESStatistikListeModtag-20160327-094743.zip.zip")
    End Sub
End Class