FTP: downloaded zip file gets corrupted (VB.net)

351 Views Asked by At

I'm trying to automatically download a large zip file once a week. So far i've managed to check if a new file is uploaded and I'm ready to start the download, but I'm running in to a few problems. The file I'm downloading is above 2.2 GB (Zip), so it's a pretty big file, and each time I test a new peace of code, I need to wait 5-10 min for the file to download again. I have a more complicated code at the bottom, and a simple code here first. Both result in the same error once the last few bytes are being transferred.

Try
    My.Computer.Network.DownloadFile(
"ftp://dmr-ftp-user:[email protected]/ESStatistikListeModtag/ESStatistikListeModtag-20160110-093818.zip",
"C:\ZipDownload\Test.zip")
Catch ex As Exception
   MsgBox(ex.Message)
End Try

Yes, this is the actual login information to the ftp host, so if you want to test your code before posting a possible solution - feel free. Once the file is just about to finish downloading i get this exception:

Exception: The underlying connection was closed: An unexpected error occurred on a receive.

I've also tried this slightly more complicated version I found online:

Private Sub Download(ByVal filePath As String, ByVal fileName As String)
        FTPSettings.IP = "5.44.137.84"
        FTPSettings.UserID = "dmr-ftp-user"
        FTPSettings.Password = "dmrpassword"
        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 Integer
            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

Both of these solutions seem to work just fine, and I can follow the file size increase on my test folder, until the very end where it freezes for a few seconds and then return the same exception:

Exception: The underlying connection was closed: An unexpected error occurred on a receive.

Hope you have an explanation to this problem!

0

There are 0 best solutions below