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

我还遗漏了什么?

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

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


当前回答

The forbidden printable ASCII characters are: Linux/Unix: / (forward slash) Windows: < (less than) > (greater than) : (colon - sometimes works, but is actually NTFS Alternate Data Streams) " (double quote) / (forward slash) \ (backslash) | (vertical bar or pipe) ? (question mark) * (asterisk) Non-printable characters If your data comes from a source that would permit non-printable characters then there is more to check for. Linux/Unix: 0 (NULL byte) Windows: 0-31 (ASCII control characters) Note: While it is legal under Linux/Unix file systems to create files with control characters in the filename, it might be a nightmare for the users to deal with such files. Reserved file names The following filenames are reserved: Windows: CON, PRN, AUX, NUL COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9 LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9 (both on their own and with arbitrary file extensions, e.g. LPT1.txt). Other rules Windows: Filenames cannot end in a space or dot. macOS: You didn't ask for it, but just in case: Colon : and forward slash / depending on context are not permitted (e.g. Finder supports slashes, terminal supports colons). (More details)

其他回答

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

$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接受了它。

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

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

禁止文件名字符的“全面指南”在Windows上不起作用,因为它保留了文件名和字符。是的,像这样的角色 *”?还有一些名字是禁止使用的,但是有无数个名字是只由有效字符组成的,是禁止使用的。例如,空格和点是有效的文件名字符,但仅由这些字符组成的名称是禁止的。

Windows不区分大写字母和小写字母,因此如果已经存在名为a的文件夹,则不能创建名为a的文件夹。更糟糕的是,像PRN和CON这样看似允许的名字,以及许多其他的名字,是被保留和不允许的。Windows也有一些长度限制;在一个文件夹中有效的文件名如果移到另一个文件夹中可能会失效。的规则 命名文件和文件夹 都在微软文档里。

一般来说,不能使用用户生成的文本创建Windows目录名。如果您希望允许用户任意命名,则必须创建安全的名称,如A、AB、A2等,将用户生成的名称及其等效路径存储在应用程序数据文件中,并在应用程序中执行路径映射。

如果您绝对必须允许用户生成文件夹名,那么判断它们是否无效的唯一方法是捕获异常并假定名称无效。即使这样也充满了危险,因为为拒绝访问、脱机驱动器和驱动器空间外抛出的异常与为无效名称抛出的异常重叠。你正在打开一个巨大的伤害罐。

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

路径。GetInvalidFileNameChars 路径。GetInvalidPathChars

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

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