Public Sub ResumeFTP(ByVal url As Uri, _
ByVal filename As String, _
ByVal cred As Net.NetworkCredential, _
ByVal offset As Long)
Dim req As Net.FtpWebRequest = Net.FtpWebRequest.Create(url) [COLOR="Green"]'creates webrequest[/COLOR]
req.Credentials = cred [COLOR="Green"]'assigns credentials[/COLOR]
req.ContentOffset = offset [COLOR="green"]'set byte offset where to start downloading[/COLOR]
Dim res As Net.FtpWebResponse = req.GetResponse [COLOR="green"]'sends request and gets response[/COLOR]
If res.StatusCode = Net.FtpStatusCode.OpeningData Then [COLOR="green"]'checks response status code[/COLOR]
Dim reStream As IO.Stream = res.GetResponseStream [COLOR="green"]'gets the data stream of response[/COLOR]
Dim fs As New IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Write) [COLOR="green"]'opens a file stream for write access[/COLOR]
fs.Position = offset [COLOR="green"]'set byte offset where to start writing[/COLOR]
CopyStream(reStream, fs) [COLOR="green"]'copies data from one stream to another[/COLOR]
fs.Close() [COLOR="green"]'close file stream[/COLOR]
reStream.Close() [COLOR="green"]'close response stream[/COLOR]
End If
res.Close() [COLOR="green"]'close response[/COLOR]
End Sub
Public Sub CopyStream(ByVal source As IO.Stream, ByVal destination As IO.Stream)
Dim read As Integer = -1 [COLOR="Green"]'tracker variable[/COLOR]
Dim buffer(8191) As Byte [COLOR="green"]'byte buffer array[/COLOR]
While read <> 0 [COLOR="green"]'while something was read[/COLOR]
read = source.Read(buffer, 0, buffer.Length) '[COLOR="green"]try to read 8192 bytes from source[/COLOR]
destination.Write(buffer, 0, read) [COLOR="green"]'write number of bytes read to destination[/COLOR]
End While
End Sub