![](/img/trans.png)
[英]Check if any file with an extension exists on SFTP server using WinSCP .NET assembly
[英]Download file chunks with offset from SFTP server using WinSCP .NET assembly
我們目前正在使用WinSCP .NET程序集與SFTP服務器進行交互。 我們的用例涉及以塊的形式獲取文件的一部分。 我看到TransferResumeSupportState
傳輸選項可用於恢復文件下載,但不能在需要時自由停止和啟動/恢復下載。
另一個用例之一不要求已下載(處理)的文件部分位於同一位置(已下載的文件的一部分已被處理,不再需要)。 要使TransferResumeSupportState
選項起作用,已下載的文件部分必須存在於同一位置。
是否有解決方法將文件偏移值傳遞給GetFiles
?
謝謝,
Vagore
作為替代方案,我會使用SSH.NET完成此任務,您可以直接在流上操作。
var client = new SftpClient(connectionInfo);
client.Connect();
var sftpFileStream = client.OpenRead(filePath);
sftpFileStream.Seek(previouslyReadOffset, SeekOrigin.Begin);
sftpFileStream.CopyTo(localStream);
WinSCP .NET程序集無法實現這一點。
你所能做的就是欺騙WinSCP
TransferOptions.OverwriteMode
設置為OverwriteMode.Resume
(請注意,它不是關於TransferResumeSupportState
)並將創建的TransferOptions
給Session.GetFiles
: long offset = 1024 * 1024;
const string remotePath = "/remote/path";
// Quickly create an dummy temporary local file with the desired size
string localPath = Path.GetTempFileName();
using (FileStream fs = File.Create(localPath))
{
fs.SetLength(offset);
}
// "Resume" the download
TransferOptions transferOptions = new TransferOptions();
transferOptions.OverwriteMode = OverwriteMode.Resume;
session.GetFiles(
RemotePath.EscapeFileMask(remotePath), localPath, false, transferOptions).Check();
// Read the downloaded chunk
byte[] chunk;
using (FileStream fs = File.OpenRead(localPath))
{
fs.Seek(offset, SeekOrigin.Begin);
int downloaded = (int)(fs.Length - offset);
chunk = new byte[downloaded];
fs.Read(chunk, 0, downloaded);
}
// Delete the temporary file
File.Delete(localPath);
SetLength
技巧基於在C#中以秒為單位創建一個巨大的虛擬文件 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.