我知道/在Linux中是非法的,下面这些在Windows中是非法的 (我认为)*。" / \ []:;|,

我还遗漏了什么?

然而,我需要一份全面的指南,一份考虑到各种因素的指南 双字节字符。链接到外部资源对我来说很好。

我需要首先在文件系统上创建一个目录,其名称可能是 包含禁用字符,所以我计划将这些字符替换为 下划线。然后,我需要将这个目录及其内容写入一个zip文件 (使用Java),因此关于zip目录名称的任何其他建议 不胜感激。


当前回答

我也有同样的需求,正在寻找推荐信或标准推荐信,偶然发现了这条线索。我目前在文件和目录名中应该避免的字符黑名单是:

$CharactersInvalidForFileName = {
    "pound" -> "#",
    "left angle bracket" -> "<",
    "dollar sign" -> "$",
    "plus sign" -> "+",
    "percent" -> "%",
    "right angle bracket" -> ">",
    "exclamation point" -> "!",
    "backtick" -> "`",
    "ampersand" -> "&",
    "asterisk" -> "*",
    "single quotes" -> "“",
    "pipe" -> "|",
    "left bracket" -> "{",
    "question mark" -> "?",
    "double quotes" -> "”",
    "equal sign" -> "=",
    "right bracket" -> "}",
    "forward slash" -> "/",
    "colon" -> ":",
    "back slash" -> "\\",
    "lank spaces" -> "b",
    "at sign" -> "@"
};

其他回答

. net框架系统。IO对于无效的文件系统字符提供如下功能:

路径。GetInvalidFileNameChars 路径。GetInvalidPathChars

这些函数应该根据. net运行时所在的平台返回适当的结果。也就是说,这些函数的文档页中的备注说:

方法返回的数组不保证包含 文件和目录中无效的完整字符集 的名字。完整的无效字符集可能因文件系统而异。

对于任何寻找正则表达式的人:

const BLACKLIST = /[<>:"\/\\|?*]/g;

在Linux和其他与unix相关的系统下,传统上只有两个字符不能出现在文件或目录的名称中,那就是NUL '\0'和斜杠'/'。当然,斜杠可以出现在路径名中,分隔目录组件。

有传言说,史蒂文·伯恩(因“贝壳”而出名)有一个包含254个文件的目录,一个文件对应一个可以出现在文件名中的每个字母(字符代码)(不包括/,'\0';名字。当然是当前的目录)。它被用来测试伯恩外壳,并经常对备份程序等不小心的程序造成严重破坏。

其他人已经介绍了Windows文件名的规则,并提供了微软和维基百科的相关链接。

注意MacOS X有一个不区分大小写的文件系统。目前的版本似乎允许在文件名中使用冒号,尽管在历史上并不一定总是这样:

$ echo a:b > a:b
$ ls -l a:b
-rw-r--r--  1 jonathanleffler  staff  4 Nov 12 07:38 a:b
$

但是,至少在macOS Big Sur 11.7中,文件系统不允许文件名不是有效的UTF-8字符串。这意味着文件名不能由UTF-8中始终无效的字节组成(0xC0, 0xC1, 0xF5-0xFF),并且不能使用延续字节0x80..0xBF作为文件名中的唯一字节。给出的错误是92非法字节序列。

POSIX定义了一个可移植文件名字符集,包括:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 . _ -

坚持使用完全由这些字符组成的名称可以避免大部分问题,不过Windows仍然增加了一些复杂性。


1

It was Kernighan & Pike in ['The Practice of Programming'](http://www.cs.princeton.edu/~bwk/tpop.webpage/) who said as much in Chapter 6, Testing, §6.5 Stress Tests:

When Steve Bourne was writing his Unix shell (which came to be known as the Bourne shell), he made a directory of 254 files with one-character names, one for each byte value except '\0' and slash, the two characters that cannot appear in Unix file names. He used that directory for all manner of tests of pattern-matching and tokenization. (The test directory was of course created by a program.) For years afterwards, that directory was the bane of file-tree-walking programs; it tested them to destruction.

注意,该目录必须包含条目。和. .,所以它可以说是253个文件(和2个目录),或者255个名称条目,而不是254个文件。这并不影响轶事的有效性,也不影响它所描述的仔细测试。

TPOP之前在 http://plan9.bell-labs.com/cm/cs/tpop和 http://cm.bell-labs.com/cm/cs/tpop但现在(2021-11-12)都坏了。 参见维基百科上的TPOP。

我也有同样的需求,正在寻找推荐信或标准推荐信,偶然发现了这条线索。我目前在文件和目录名中应该避免的字符黑名单是:

$CharactersInvalidForFileName = {
    "pound" -> "#",
    "left angle bracket" -> "<",
    "dollar sign" -> "$",
    "plus sign" -> "+",
    "percent" -> "%",
    "right angle bracket" -> ">",
    "exclamation point" -> "!",
    "backtick" -> "`",
    "ampersand" -> "&",
    "asterisk" -> "*",
    "single quotes" -> "“",
    "pipe" -> "|",
    "left bracket" -> "{",
    "question mark" -> "?",
    "double quotes" -> "”",
    "equal sign" -> "=",
    "right bracket" -> "}",
    "forward slash" -> "/",
    "colon" -> ":",
    "back slash" -> "\\",
    "lank spaces" -> "b",
    "at sign" -> "@"
};

I always assumed that banned characters in Windows filenames meant that all exotic characters would also be outlawed. The inability to use ?, / and : in particular irked me. One day I discovered that it was virtually only those chars which were banned. Other Unicode characters may be used. So the nearest Unicode characters to the banned ones I could find were identified and MS Word macros were made for them as Alt+?, Alt+: etc. Now I form the filename in Word, using the substitute chars, and copy it to the Windows filename. So far I have had no problems.

下面是替换字符(Alt +十进制Unicode):

⃰ ⇔ Alt8432 ⁄ ⇔ Alt8260 ⃥ ⇔ Alt8421 ∣ ⇔ Alt8739 ⵦ ⇔ Alt11622 ⮚ ⇔ Alt11162 ‽ ⇔ Alt8253 ፡ ⇔ Alt4961 ‵‵ ⇔ Alt8246 “ ⇔ Alt8243

作为测试,我用所有这些字符组成了一个文件名,Windows接受了它。