带通配符的 FTP 目录部分列表

发布于 2025-01-05 18:41:52 字数 327 浏览 0 评论 0原文

首先我问: ftp 目录列出超时。大量的子目录。我得到了答案。

尽管如此,因为目录中可能有数十万个 FTP 对象,所以扫描它可能需要很长时间。不过,我认为可能检索以“A”开头,然后是“B”等的所有对象...当它检索目录时,它可以开始在另一个线程上处理它们,而无需等到它获取整个列表。

是否可以使用标准 FtpWebRequest 使用通配符列出 FTP 目录?

First I asked that: ftp directory listing timeout. Huge number of subdirs. I got the answer.

Still because I can have hundred thousands of FTP objects in the directory it could take really long time to scan that. However I thought it might be possible to retrieve all the objects that begin with 'A' and then 'B' and so on... As it retrieves directories it could start processing them on the other thread without waiting till it gets the entire list.

Is it possible to do FTP directory listing with wildcards using standard FtpWebRequest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你对谁都笑 2025-01-12 18:41:52

FTP 规范 (RFC 3659) 的最新更新明确禁止它。来自该规范的第 2.2.2 节,标题为“通配符”(强调我的):

对于本规范中定义的命令,所有路径名都是
按字面意思对待。也就是说,对于作为参数给出的路径名
一个命令,其名称与给定路径名相同的文件是
暗示的。路径名中的任何字符都不能被视为特殊或
“神奇”,因此没有模式匹配(除了完全相等)
给定的路径名​​与 NVFS 中存在的文件之间
允许服务器 FTP。

需要某种形式模式的客户
匹配功能必须获得相关的列表
一个或多个目录,并实现自己的文件名
选择程序。

也就是说,如果您的服务器支持它,您仍然可以使用 FtpWebRequest,但您必须自己处理响应才能处理项目列表,因为 .NET 类无法理解您的服务器 -具体扩展。

The most recent update to the FTP specification (RFC 3659) explicitly forbids it. From section 2.2.2 of that specification, titled "Wildcarding" (emphasis mine):

For the commands defined in this specification, all pathnames are to
be treated literally. That is, for a pathname given as a parameter to
a command, the file whose name is identical to the pathname given is
implied. No characters from the pathname may be treated as special or
"magic", thus no pattern matching (other than for exact equality)
between the pathname given and the files present in the NVFS of the
server-FTP is permitted.

Clients that desire some form of pattern
matching functionality must obtain a listing of the relevant
directory, or directories, and implement their own file name
selection procedures.

That said, if your server supports it, you could still use the FtpWebRequest class, but you'd have to process the response yourself to handle the list of items, as the .NET classes won't understand your server-specific extensions.

楠木可依 2025-01-12 18:41:52

FTP 规范规定文件列表命令(LISTNLISTMLSD 等)的参数是路径名。所以无论如何都不应该有通配符。

RFC 959 (LIST + NLIST):

2.2。术语

...

路径名

路径名被定义为字符串,必须是
用户输入到文件系统以识别文件。
路径名通常包含设备和/或目录名称,并且
文件名规范。 FTP 尚未指定标准
路径名约定。每个用户必须遵循文件命名
传输涉及的文件系统的约定。

...

5.3.1。 FTP 命令

...

列表 [<路径名>]
NLST [<路径名>]

RFC 3659 (MLSD):

2.2.2。通配符

对于本规范中定义的命令,所有路径名都是
按字面意思对待。也就是说,对于作为参数给出的路径名
对于命令,名称与给定路径名相同的文件
是暗示的。路径名中的任何字符都不能被视为
特殊或“魔法”,因此没有模式匹配(除了精确匹配)
相等)给定的路径名​​和中存在的文件之间
允许服务器-FTP的NVFS。

...

7.1。 MLSx请求的格式

...

MLSx 命令的语法是:

mlst = "MLst" [ SP 路径名 ] CRLF
mlsd = "MLsD" [ SP 路径名 ] CRLF

实际上,尽管许多 FTP 服务器确实支持参数中的通配符。但由于规范不允许这样做,因此显然对于支持的通配符没有设定标准。

vsftpd 支持 *?{} 以及 LIST。 vsftpd 不支持现代的 MLSD。

proftpd 支持*?[]。但仅适用于LIST。它明确不允许使用带有注释的现代 MLSD 通配符:

RFC3659 明确不支持全局字符。所以警告
这,但让命令继续按原样。

pureftpd 支持 *?[] 用于 LIST 和 <代码>MLSD。

FileZilla 服务器 仅支持LISTMLSD*


但一般来说,您根本不应该依赖 FTP 服务器来支持任何通配符。

唯一可靠的方法是检索完整的目录列表并在本地过滤文件。例如,您可以使用正则表达式(正则表达式

The FTP specification says that the argument to file listing commands (LIST, NLIST, MLSD, etc) is a pathname. So there should be NO wildcard, whatsoever.

RFC 959 (LIST + NLIST):

2.2. TERMINOLOGY

...

pathname

Pathname is defined to be the character string which must be
input to a file system by a user in order to identify a file.
Pathname normally contains device and/or directory names, and
file name specification. FTP does not yet specify a standard
pathname convention. Each user must follow the file naming
conventions of the file systems involved in the transfer.

...

5.3.1. FTP COMMANDS

...

LIST [<SP> <pathname>] <CRLF>
NLST [<SP> <pathname>] <CRLF>

RFC 3659 (MLSD):

2.2.2. Wildcarding

For the commands defined in this specification, all pathnames are to
be treated literally. That is, for a pathname given as a parameter
to a command, the file whose name is identical to the pathname given
is implied. No characters from the pathname may be treated as
special or "magic", thus no pattern matching (other than for exact
equality) between the pathname given and the files present in the
NVFS of the server-FTP is permitted.

...

7.1. Format of MLSx Requests

...

The syntax for the MLSx command is:

mlst             = "MLst" [ SP pathname ] CRLF
mlsd             = "MLsD" [ SP pathname ] CRLF

In practice though many FTP servers do support wildcards in the argument. But as the specification does not allow that, there's obviously no set standard for the wildcards supported.

vsftpd supports *, ? and {} with the LIST. vsftpd does not support the modern MLSD.

proftpd supports *, ? and []. But for the LIST only. It explicitly does not allow wildcards with the modern MLSD with a comment:

RFC3659 explicitly does NOT support glob characters. So warn about
this, but let the command continue as is.

pureftpd supports *, ? and [] for both the LIST and the MLSD.

FileZilla server supports * only for both the LIST and the MLSD.


But in general, you should not rely on the FTP server to support any wildcards at all.

The only reliable approach to is to retrieve a complete directory listing and filter the files locally. For example you can use a regular expression (the Regex class)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文