# 文件扩展名

文件扩展名是一种后缀，通常用句点与文件名的其余部分隔开，加在计算机文件名的末尾，用来表明文件的类型或格式。例如在 `expenses.xlsx` 中，主文件名是 `expenses`，扩展名是 `xlsx`<sup>[1](https://support.microsoft.com/en-us/windows/experience/storage-filemanagement/common-file-name-extensions-in-windows)</sup>。扩展名是一种命名约定，而不是文件内容本身的属性：究竟什么算作扩展名，具体规则属于特定的文件系统，通常它是名称中最后一个句点之后的子串<sup>[2](https://www.gammasolutions.com/wp-content/uploads/pdf/E-INF-GLOSSARY.pdf)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 文件主文件名末尾、句点之后的一小段字符串，用于表明文件类型<sup>[3](https://www.linfo.org/filename_extension.html)</sup> |
| 典型长度 | 在 Windows 中为三到四个字符；历史上在 DOS 的 8.3 方案下限于三个字符<sup>[1](https://support.microsoft.com/en-us/windows/experience/storage-filemanagement/common-file-name-extensions-in-windows)</sup><sup> • </sup><sup>[4](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/18e63b13-ba43-4f5f-a5b7-11e871b71f14)</sup> |
| 操作系统角色 | 在 MS-DOS 和 Windows 中是可选的，在那里它们可用于标识文件类型或帮助将文件与应用程序关联；类 Unix 系统通常不要求扩展名，尽管某些应用程序会要求<sup>[3](https://www.linfo.org/filename_extension.html)</sup><sup> • </sup><sup>[19](https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file)</sup> |
| Windows 关联 | HKEY_CLASSES_ROOT 中的扩展名子键将扩展名映射到 ProgID，后者指明处理该扩展名的应用程序<sup>[5](https://learn.microsoft.com/en-us/windows/win32/shell/fa-file-types)</sup> |
| Web 处理 | 浏览器使用 Content-Type 标头中发送的 MIME 类型（而非文件扩展名）来决定如何处理 URL<sup>[6](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types)</sup> |
| 安全相关性 | 隐藏扩展名可让 `vulnerability_assessment.pdf.exe` 这样的文件显示为 `vulnerability_assessment.pdf`，从而助长社会工程<sup>[7](https://www.cyber.gov.au/sites/default/files/2024-07/PROTECT%20-%20Hardening%20Microsoft%20Windows%2010%20and%20Windows%2011%20Workstations%20%28July%202024%29.pdf)</sup> |

## 用途与行为

扩展名告诉操作系统和用户，文件由哪个应用程序创建或可由哪个应用程序打开，以及应为它显示哪个图标。例如，`docx` 扩展名告诉 Windows：[Microsoft Word](https://www.edgepedia.org/zh/microsoft-word) 可以打开该文件，并且文件资源管理器中应显示 Word 图标<sup>[1](https://support.microsoft.com/en-us/windows/experience/storage-filemanagement/common-file-name-extensions-in-windows)</sup>。许多系统仅凭 `.jpg`、`.mp3` 和 `.html` 等常见扩展名来定义文档类型；这类系统可能把没有扩展名的文件视为未知类型<sup>[8](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFileSystem/Articles/FilenameExtensions.html)</sup>。

重命名文件的扩展名只会改变名称。它不会把文件转换为另一种格式，也不会改变其内容<sup>[1](https://support.microsoft.com/en-us/windows/experience/storage-filemanagement/common-file-name-extensions-in-windows)</sup>。一个文件也可以带有多个扩展名：在类 Unix 系统上用于分发压缩程序的 `.tar.gz` 和 `.tar.bz2` 就是常见例子<sup>[3](https://www.linfo.org/filename_extension.html)</sup>。

## 历史与 8.3 限制

三字符扩展名源自早期微型计算机操作系统。PC/MS-DOS 从 CP/M 继承了 8.3 命名限制，而 CP/M 自己的 6.3 方案之所以被选中，部分是为了兼容 IBM 的 8 字符磁带和磁盘文件名<sup>[9](https://retrocomputing.stackexchange.com/questions/23917/origin-of-the-8-3-file-names-scheme)</sup>。在 8.3 方案下，有效文件名由 1 到 8 个字符的主文件名组成，后面可选地跟一个句点和一个 1 到 3 个字符的扩展名；名称必须使用低于 0x80 的 ASCII 字符，不得包含空格，也不得包含一个以上的句点<sup>[4](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/18e63b13-ba43-4f5f-a5b7-11e871b71f14)</sup>。超出限制输入的字符会被直接忽略，因此在 DOS 中，`my_new_novel.txt` 会变成 `my_new_n.txt`<sup>[10](https://www.dosdays.co.uk/topics/dos_8_3.php)</sup>。

PDP-11 上的早期 UNIX 则以另一种方式更为严格：文件名限于 8 个字符，没有为扩展名作特殊规定，因此任何句点都只是存储名称的一部分<sup>[9](https://retrocomputing.stackexchange.com/questions/23917/origin-of-the-8-3-file-names-scheme)</sup>。

## 操作系统差异

**Windows。** MS-DOS 和 Windows 系统要求扩展名，系统才能运作，几乎每个文件都有一个扩展名<sup>[3](https://www.linfo.org/filename_extension.html)</sup>。文件关联通过注册表实现：以扩展名命名的子键（例如 `.myp`）保存一个默认值，给出处理该扩展名的应用程序的 ProgID；这些内容存储在 HKEY_CLASSES_ROOT 子树中<sup>[5](https://learn.microsoft.com/en-us/windows/win32/shell/fa-file-types)</sup>。微软给开发者的指南倾向于使用长扩展名而非短扩展名，因为短扩展名的长度有限，容易发生扩展名冲突，即同一个扩展名被用来归类多种文件类型<sup>[11](https://learn.microsoft.com/en-us/windows/win32/shell/fa-best-practices)</sup>。

默认情况下，Windows 会在[文件资源管理器](https://www.edgepedia.org/zh/file-explorer)和桌面上隐藏已知文件类型的扩展名。此行为由 `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced` 下的 `HideFileExt` 值控制；安全加固指南建议将其设为 0，以显示扩展名<sup>[7](https://www.cyber.gov.au/sites/default/files/2024-07/PROTECT%20-%20Hardening%20Microsoft%20Windows%2010%20and%20Windows%2011%20Workstations%20%28July%202024%29.pdf)</sup>。即使禁用该设置，某些文件扩展名仍会保持隐藏<sup>[12](https://repository.root-me.org/Administration/Microsoft/EN%20-%20NIST%20Guide%20to%20securing%20microsoft%20windows%20XP%20systems.pdf)</sup>。

**类 Unix 系统。** Linux 和其他类 Unix 操作系统通常不要求扩展名，尽管运行在其上的一些应用程序会要求；例如，C 编译器要求源文件具有 `.c` 扩展名，头文件具有 `.h` 扩展名<sup>[3](https://www.linfo.org/filename_extension.html)</sup>。在 freedesktop.org 共享 MIME 信息数据库中，应用程序默认以不区分大小写的方式将文件名 glob 模式与 MIME 类型匹配，优先选择最长匹配模式，因此 `Data.tar.gz` 会匹配 `*.tar.gz` 而不是 `*.gz`；当 glob 匹配失败或多种模式冲突时，则回退到内容嗅探（魔数数据）<sup>[13](https://specifications.freedesktop.org/shared-mime-info/latest-single/)</sup>。

**macOS。** 经典 Macintosh 系统将文件类型信息存储在文件系统元数据中，而不是存储在名称中。现代 macOS 将扩展名映射到统一类型标识符（UTI）：应用程序在其 `Info.plist` 中声明文档类型，在 `UTTypeTagSpecification` 字典的 `public.filename-extension` 键下列出扩展名<sup>[14](https://developer.apple.com/documentation/uniformtypeidentifiers/defining-file-and-data-types-for-your-app?changes=_1_5)</sup>。Mac OS X 还允许按文件隐藏扩展名；这只会改变显示名称，不会改变文件在文件系统中的实际名称<sup>[8](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFileSystem/Articles/FilenameExtensions.html)</sup>。

## Web 上的扩展名

在 Web 上，扩展名并不具有权威性。浏览器使用 MIME 类型而非文件扩展名来决定如何处理 URL，因此 Web 服务器必须在响应的 `Content-Type` 标头中发送正确的类型；配置错误可能导致浏览器误读文件内容<sup>[6](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types)</sup>。官方 MIME 类型由[互联网号码分配局](https://www.edgepedia.org/zh/internet-assigned-numbers-authority)（IANA）注册，微软建议定义新的公共文件类型的开发者也向 IANA 注册 MIME 类型<sup>[6](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types)</sup><sup> • </sup><sup>[11](https://learn.microsoft.com/en-us/windows/win32/shell/fa-best-practices)</sup>。

## 安全

由于扩展名只是名称的一部分，它可以被伪造；在安全实践中，扩展名和魔数字节（文件开头用于标识格式的签名字节）都被视为不可信输入<sup>[15](https://arxiv.org/abs/1002.3174)</sup>。有两种反复出现的滥用模式：

- **双重扩展名。** 恶意可执行文件被命名时附加一个看起来无害的扩展名，例如 `cv.pdf.exe`，以误导用户和安全控制机制对文件真实类型的判断；这利用了操作系统、电子邮件客户端和用户解释文件名的方式<sup>[16](https://www.ccinfo.nl/_downloads/99683243dd0eed95e4def3c7bb13c5f2)</sup>。[澳大利亚信号局](https://www.edgepedia.org/zh/australian-signals-directorate)的加固指南给出了 `vulnerability_assessment.pdf.exe` 的例子：当扩展名被隐藏时，它显示为 `vulnerability_assessment.pdf`；该指南建议对所有已知文件类型显示扩展名，并配合用户教育，以降低用户执行恶意电子邮件附件的风险<sup>[7](https://www.cyber.gov.au/sites/default/files/2024-07/PROTECT%20-%20Hardening%20Microsoft%20Windows%2010%20and%20Windows%2011%20Workstations%20%28July%202024%29.pdf)</sup>。
- **上传过滤绕过。** 仅通过扩展名验证上传的 Web 应用程序，可能被解析差异、大小写敏感性不匹配，或对句点和斜杠进行 URL 编码所绕过；因此建议将基于内容的检查作为更强的控制措施<sup>[17](https://sclsc.gov.in/uploads/1666518127382-30012023060130.pdf)</sup>。

检测系统越来越不只看名称。基于内容的文件类型识别——包括经过训练、即使扩展名或文件头被混淆也能判断文件真实类型的机器学习分类器——是恶意软件初筛与取证中的一个活跃领域<sup>[15](https://arxiv.org/abs/1002.3174)</sup><sup> • </sup><sup>[18](https://arxiv.org/abs/2301.11964)</sup>。

## References

1. [Common file name extensions in Windows, Microsoft Support](https://support.microsoft.com/en-us/windows/experience/storage-filemanagement/common-file-name-extensions-in-windows)
2. [Technical Glossary, Gamma Solutions](https://www.gammasolutions.com/wp-content/uploads/pdf/E-INF-GLOSSARY.pdf)
3. [Filename Extension Definition, The Linux Information Project (LINFO)](https://www.linfo.org/filename_extension.html)
4. [MS-FSCC: 8.3 Filename, Microsoft Learn](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/18e63b13-ba43-4f5f-a5b7-11e871b71f14)
5. [File Types (Win32), Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/shell/fa-file-types)
6. [Media types (MIME types), MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types)
7. [PROTECT: Hardening Microsoft Windows 10 and Windows 11 Workstations (July 2024), Australian Signals Directorate](https://www.cyber.gov.au/sites/default/files/2024-07/PROTECT%20-%20Hardening%20Microsoft%20Windows%2010%20and%20Windows%2011%20Workstations%20%28July%202024%29.pdf)
8. [Filename Extensions, Apple Developer Documentation](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFileSystem/Articles/FilenameExtensions.html)
9. [Origin of the 8.3 file names scheme, Retrocomputing Stack Exchange](https://retrocomputing.stackexchange.com/questions/23917/origin-of-the-8-3-file-names-scheme)
10. [DOS 8.3 Filenames, DOS Days](https://www.dosdays.co.uk/topics/dos_8_3.php)
11. [Best Practices for File Associations (Win32), Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/shell/fa-best-practices)
12. [NIST Special Publication 800-68: Guide to Securing Microsoft Windows XP Systems](https://repository.root-me.org/Administration/Microsoft/EN%20-%20NIST%20Guide%20to%20securing%20microsoft%20windows%20XP%20systems.pdf)
13. [Shared MIME-info Database specification, freedesktop.org](https://specifications.freedesktop.org/shared-mime-info/latest-single/)
14. [Defining file and data types for your app, Apple Developer Documentation](https://developer.apple.com/documentation/uniformtypeidentifiers/defining-file-and-data-types-for-your-app?changes=_1_5)
15. [A new approach to content-based file type detection, arXiv](https://arxiv.org/abs/1002.3174)
16. [Picus Red Report 2026 (double file extensions technique)](https://www.ccinfo.nl/_downloads/99683243dd0eed95e4def3c7bb13c5f2)
17. [Server side topic (file extension obfuscation), SCLSC training material](https://sclsc.gov.in/uploads/1666518127382-30012023060130.pdf)
18. [Adversarial Networks and Machine Learning for File Classification, arXiv](https://arxiv.org/abs/2301.11964)
19. [Naming Files, Paths, and Namespaces - Win32 apps](https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Data formats and serialization › Text, encodings, and archives

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/filename-extension

English original: https://www.edgepedia.org/filename-extension
