本文介绍了一次调用即可递归获取所有FTP目录/文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在创建一个将数据备份到FTP的备份程序.为了有效归档,我需要进行几个文件属性比较.现在,我正在使用 FluentFTP 并调用 FtpClient.FileExists FtpClient.GetFileSize FtpClient.GetModifiedTime 每个文件.这显然不是很理想.

I'm creating a backup program that backs data up to FTP. To archive effectively, I need to do several file attribute comparisons. Right now, I am using FluentFTP and calling FtpClient.FileExists FtpClient.GetFileSize, and FtpClient.GetModifiedTime per file. This is obviously not very optimal.

如果我可以在一次调用中下载FTP目录的整个树结构以及文件属性,它将大大提高备份速度.另一种方法是建立一个本地索引,但随后我必须确保它已正确更新,并且还要考虑到它损坏的可能性.

If I could download the FTP directory's entire tree structure in a single call along with file attributes, it would improve back-up speed substantially. The alternative is to build a local index, but then I have to make sure it's being updated properly and also account for the possibility of it getting corrupted.

除了推出自己的解决方案之外,还有其他方法吗?

Is there any way to do this other than roll my own solution?

推荐答案

FTP提供了以下命令来检索有关远程文件的信息:

FTP offers these commands to retrieve an information about remote files:

  • NLST 来检索特定目录中的文件名-受所有服务器支持.
  • SIZE MDTM 来检索特定文件的大小和修改文件–由实际上所有服务器支持.
  • LIST 来检索目录列表,包括文件属性–由所有服务器支持,但是该列表不是标准化的,因此不是真正的机器可读的.尽管大多数服务器将支持* nix样式列表(例如* nix ls 命令).尽管每个人都有自己的怪癖.
  • MLSD 来检索目录列表,包括机器可读格式的文件属性–最少支持此命令.* nix vsftpd和Windows IIS尤其不支持它.
  • NLST to retrieve names of files in certain directory – Supported by all servers.
  • SIZE and MDTM to retrieve size and modification file of certain file – Supported by virtually all servers.
  • LIST to retrieve directory listing, including file attributes – Supported by all servers, but the listing is not standardized and thus not really machine-readable. Though most servers will support *nix style listing (like *nix ls command). Though each will have its quirks.
  • MLSD to retrieve directory listing, including file attributes in machine readable format – This command is least supported. It's particularly not supported by *nix vsftpd and Windows IIS.

因此您不必使用 FtpClient.GetFileSize FtpClient.GetModifiedTime 每个文件.使用 FtpClient.GetListing 每个目录.如果服务器支持,它内部使用 MLSD .否则,它会退回到 LIST 并尝试解析列表.

So you do not have to use FtpClient.GetFileSize and FtpClient.GetModifiedTime per file. Use FtpClient.GetListing per directory. Internally it uses MLSD, if supported by the server. Otherwise it falls back to LIST and tries to parse the listing.

某些FTP服务器(例如ProFTPD)确实支持将非标准专有的 -R 切换到 LIST 命令,这将使它们在所有子文件夹中返回列表.FluentFTP也支持该功能( FtpListOption.Recursive ).但是请注意,FluentFTP仅将 -R LIST 一起使用,而如果服务器支持,则它更倾向于使用 MLSD .因此,要确保使用 LIST -R ,您需要同时使用 FtpListOption.Recursive FtpListOption.ForceList .

Some FTP servers (like ProFTPD) do support a non-standard proprietary -R switch to the LIST command that will make them return listing across all subfolders. FluentFTP supports that too (FtpListOption.Recursive). Though note that FluentFTP uses -R with LIST only, while it prefers using MLSD, if the server supports it. So to make sure LIST -R is used, you need to use both FtpListOption.Recursive and FtpListOption.ForceList.

如果服务器不支持 -R 开关,则必须自己实现递归.或使用具有API的FTP客户端.

If your server does not support the -R switch, you have to implement recursion yourself. Or use an FTP client that has API for it.

例如,使用 my WinSCP .NET程序集,您可以使用 Session.EnumerateRemoteFiles :

For example with my WinSCP .NET assembly, you can use Session.EnumerateRemoteFiles:

IEnumerable<RemoteFileInfo> allFiles =
    session.EnumerateRemoteFiles("/", null, EnumerationOptions.AllDirectories);

这篇关于一次调用即可递归获取所有FTP目录/文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:13