You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

Imports System.IO
Imports System.Text.RegularExpressions
Imports Microsoft.VisualBasic.Net
Imports Microsoft.VisualBasic.Net.Protocols
Imports Microsoft.VisualBasic.Net.Protocols.Reflection
Imports Microsoft.VisualBasic.Serialization
Namespace FileSystem.Protocols
Public Module NetTransfer
''' <summary>
''' 从本地上传文件
''' </summary>
''' <param name="local"></param>
''' <param name="destination"></param>
''' <param name="remote"></param>
Public Sub Upload(local As String, destination As String, remote As IPEndPoint)
Using file As New IO.RemoteFileStream(destination, FileMode.OpenOrCreate, remote)
Dim localFile As New FileStream(local, FileMode.Open)
Call Transfer(localFile, file, 1024 * 1024)
End Using
End Sub
Public Sub Transfer(source As Stream, target As Stream, bufLen As Integer)
Dim buffer As Byte() = New Byte(bufLen - 1) {}
Do While source.Position < source.Length
Dim d As Integer = (source.Length - source.Position) - buffer.Length
If d < 0 Then ' 注意d 是负值的
buffer = New Byte(-d - 1) {}
End If
Call source.Read(buffer, Scan0, buffer.Length)
Call target.Write(buffer, Scan0, buffer.Length)
Call target.Flush()
Loop
End Sub
Public Sub Download(destination As String, local As String, remote As IPEndPoint)
Using file As New IO.RemoteFileStream(destination, FileMode.OpenOrCreate, remote)
Dim localFile As New FileStream(local, FileMode.OpenOrCreate)
Call Transfer(file, localFile, 1024 * 1024)
End Using
End Sub
End Module
End Namespace