sindresorhus/file-type

GitHub: sindresorhus/file-type

基于二进制签名检测文件真实类型的跨平台 JavaScript 库,支持流式处理和 100 多种文件格式。

Stars: 4231 | Forks: 394

file-type logo

文件类型是通过检查缓冲区的[magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files)来检测的。 此包用于检测基于二进制的文件格式,而不是基于文本的格式,如 `.txt`、`.csv`、`.svg` 等。 我们接受针对常用现代文件格式的贡献,而不是历史或冷门的格式。请先开启一个 issue 进行讨论。 ## 安装 ``` npm install file-type ``` **此包是一个 ESM 包。您的项目也需要是 ESM。[阅读更多](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)。对于 TypeScript + CommonJS,请参阅 [`load-esm`](https://github.com/Borewit/load-esm)。** 如果您将其与 Webpack 一起使用,您需要最新版本的 Webpack 并确保正确配置 ESM。 文件类型检测基于二进制签名(magic numbers),应将其视为一种尽力而为的提示,而非保证。 ## 用法 ### Node.js 从文件确定文件类型: ``` import {fileTypeFromFile} from 'file-type'; console.log(await fileTypeFromFile('Unicorn.png')); //=> {ext: 'png', mime: 'image/png'} ``` 从 Uint8Array/ArrayBuffer 确定文件类型,这可能是文件开头的一部分: ``` import {fileTypeFromBuffer} from 'file-type'; import {readChunk} from 'read-chunk'; const buffer = await readChunk('Unicorn.png', {length: 4100}); console.log(await fileTypeFromBuffer(buffer)); //=> {ext: 'png', mime: 'image/png'} ``` 从流确定文件类型: ``` import fs from 'node:fs'; import {fileTypeFromStream} from 'file-type'; const stream = fs.createReadStream('Unicorn.mp4'); console.log(await fileTypeFromStream(stream)); //=> {ext: 'mp4', mime: 'video/mp4'} ``` 流方法也可用于从远程位置读取: ``` import got from 'got'; import {fileTypeFromStream} from 'file-type'; const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg'; const stream = got.stream(url); console.log(await fileTypeFromStream(stream)); //=> {ext: 'jpg', mime: 'image/jpeg'} ``` 另一个流示例: ``` import stream from 'node:stream'; import fs from 'node:fs'; import crypto from 'node:crypto'; import {fileTypeStream} from 'file-type'; const read = fs.createReadStream('encrypted.enc'); const decipher = crypto.createDecipheriv(alg, key, iv); const streamWithFileType = await fileTypeStream(stream.pipeline(read, decipher)); console.log(streamWithFileType.fileType); //=> {ext: 'mov', mime: 'video/quicktime'} const write = fs.createWriteStream(`decrypted.${streamWithFileType.fileType.ext}`); streamWithFileType.pipe(write); ``` ### 浏览器 ``` import {fileTypeFromStream} from 'file-type'; const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg'; const response = await fetch(url); const fileType = await fileTypeFromStream(response.body); console.log(fileType); //=> {ext: 'jpg', mime: 'image/jpeg'} ``` ## API ### fileTypeFromBuffer(buffer, options) 检测 `Uint8Array` 或 `ArrayBuffer` 的文件类型。 文件类型是通过检查缓冲区的 [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) 来检测的。 如果可以进行文件访问,建议改用 `fileTypeFromFile()`。 返回一个 `Promise`,包含检测到的文件类型的对象: - `ext` - [支持的文件类型](#supported-file-types)之一 - `mime` - [MIME 类型](https://en.wikipedia.org/wiki/Internet_media_type) 如果没有匹配项,则为 `undefined`。 #### buffer 类型:`Uint8Array | ArrayBuffer` 表示文件数据的缓冲区。如果缓冲区包含整个文件,效果最好。它也可能适用于较小的部分。 ### fileTypeFromFile(filePath, options) 检测文件路径的文件类型。 这仅适用于 Node.js。 要从 [`File`](https://developer.mozilla.org/docs/Web/API/File) 读取,请参阅 [`fileTypeFromBlob()`](#filetypefromblobblob-options)。 文件类型是通过检查缓冲区的 [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) 来检测的。 返回一个 `Promise`,包含检测到的文件类型的对象: - `ext` - [支持的文件类型](#supported-file-types)之一 - `mime` - [MIME 类型](https://en.wikipedia.org/wiki/Internet_media_type) 如果没有匹配项,则为 `undefined`。 #### filePath 类型:`string` 要解析的文件路径。 ### fileTypeFromStream(stream) 检测 [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) 的文件类型。 如果引擎是 Node.js,这也可以是 [Node.js `stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable)。 未来将放弃对 Node.js 流的直接支持,届时 Node.js 流可以转换为 Web 流(参见 [`toWeb()`](https://nodejs.org/api/stream.html#streamreadabletowebstreamreadable-options))。 文件类型是通过检查缓冲区的 [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) 来检测的。 返回一个 `Promise`,包含检测到的文件类型的对象: - `ext` - [支持的文件类型](#supported-file-types)之一 - `mime` - [MIME 类型](https://en.wikipedia.org/wiki/Internet_media_type) 如果没有匹配项,则为 `undefined`。 #### stream 类型:[Web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) 或 [Node.js `stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable) 表示文件数据的可读流。 ### fileTypeFromBlob(blob, options) 检测 [`Blob`](https://developer.mozilla.org/docs/Web/API/Blob) 的文件类型, 它将**流式传输**底层的 Blob。 文件类型是通过检查 blob 的 [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) 来检测的。 返回一个 `Promise`,包含检测到的文件类型的对象: - `ext` - [支持的文件类型](#supported-file-types)之一 - `mime` - [MIME 类型](https://en.wikipedia.org/wiki/Internet_media_type) 如果没有匹配项,则为 `undefined`。 ``` import {fileTypeFromBlob} from 'file-type'; const blob = new Blob([''], { type: 'text/plain', endings: 'native' }); console.log(await fileTypeFromBlob(blob)); //=> {ext: 'txt', mime: 'text/plain'} ``` #### blob 类型:[`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) ### fileTypeFromTokenizer(tokenizer, options) 从 `ITokenizer` 源检测文件类型。 此方法在内部使用,但也可用于特殊的“tokenizer”读取器。 tokenizer 传播内部读取函数,允许实现和使用替代的传输机制来访问文件。 返回一个 `Promise`,包含检测到的文件类型的对象: - `ext` - [支持的文件类型](#supported-file-types)之一 - `mime` - [MIME 类型](https://en.wikipedia.org/wiki/Internet_media_type) 如果没有匹配项,则为 `undefined`。 一个例子是 [`@tokenizer/http`](https://github.com/Borewit/tokenizer-http),它使用 [HTTP-range-requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests) 请求数据。与传统流和 [*tokenizer*](https://github.com/Borewit/strtok3#tokenizer) 的区别在于,它可以在流中*忽略*(seek,快进)。例如,您可能只需要读取前 6 个字节和最后 128 个字节,这在读取整个文件需要更长时间的情况下可能是一个优势。 ``` import {makeTokenizer} from '@tokenizer/http'; import {fileTypeFromTokenizer} from 'file-type'; const audioTrackUrl = 'https://test-audio.netlify.com/Various%20Artists%20-%202009%20-%20netBloc%20Vol%2024_%20tiuqottigeloot%20%5BMP3-V2%5D/01%20-%20Diablo%20Swing%20Orchestra%20-%20Heroines.mp3'; const httpTokenizer = await makeTokenizer(audioTrackUrl); const fileType = await fileTypeFromTokenizer(httpTokenizer); console.log(fileType); //=> {ext: 'mp3', mime: 'audio/mpeg'} ``` 或者使用 [`@tokenizer/s3`](https://github.com/Borewit/tokenizer-s3) 来确定存储在 [Amazon S3](https://aws.amazon.com/s3) 上的文件的文件类型: ``` import {S3Client} from '@aws-sdk/client-s3'; import {makeChunkedTokenizerFromS3} from '@tokenizer/s3'; import {fileTypeFromTokenizer} from 'file-type'; // Initialize the S3 client // Initialize S3 client const s3 = new S3Client(); // Initialize the S3 tokenizer. const s3Tokenizer = await makeChunkedTokenizerFromS3(s3, { Bucket: 'affectlab', Key: '1min_35sec.mp4' }); // Figure out what kind of file it is. const fileType = await fileTypeFromTokenizer(s3Tokenizer); console.log(fileType); ``` 请注意,只会读取确定文件类型所需的最小数据量(好吧,只是为了防止过多碎片化读取而多读一点点)。 #### tokenizer 类型:[`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) 实现 [tokenizer 接口](https://github.com/Borewit/strtok3#tokenizer)的文件源。 ### fileTypeStream(webStream, options?) 返回一个 `Promise`,解析为原始可读流参数,但添加了一个 `fileType` 属性,该属性是一个类似于 `fileTypeFromFile()` 返回的对象。 此方法可以方便地置于流之间,但这需要付出代价。 在内部,`stream()` 建立一个 `sampleSize` 字节的缓冲区,用作样本以确定文件类型。 样本大小会影响文件检测的分辨率。 较小的样本大小将导致检测出最佳文件类型的概率较低。 **注意:** 使用 Node.js 时,也可以提供 `stream.Readable`。 #### readableStream 类型:[`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable) #### options 类型:`object` 除了[常规选项](#options)外,还支持以下选项: ##### sampleSize 类型:`number`\ 默认值:`4100` 样本大小(以字节为单位)。 #### 示例 ``` import got from 'got'; import {fileTypeStream} from 'file-type'; const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg'; const stream1 = got.stream(url); const stream2 = await fileTypeStream(stream1, {sampleSize: 1024}); if (stream2.fileType?.mime === 'image/jpeg') { // stream2 can be used to stream the JPEG image (from the very beginning of the stream) } ``` #### readableStream 类型:[`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable) 输入流。 ### supportedExtensions 返回支持的文件扩展名的 `Set`。 ### supportedMimeTypes 返回支持的 MIME 类型的 `Set`。 ### 选项 #### customDetectors 在默认检测器之前运行的自定义文件类型检测器数组。 例如: ``` import {fileTypeFromFile} from 'file-type'; import {detectXml} from '@file-type/xml'; const fileType = await fileTypeFromFile('sample.kml', {customDetectors: [detectXml]}); console.log(fileType); ``` #### mpegOffsetTolerance 默认值:`0` 指定定位第一个 MPEG 音频帧(例如 `.mp1`、`.mp2`、`.mp3`、`.aac`)的字节容差。 允许检测处理预期帧起始位置与实际帧起始位置之间的轻微同步偏移。这在格式错误或不正确混合的文件中很常见,虽然技术上无效,但在实际中确实存在。 10 字节的容差可以覆盖大多数情况。 ## 自定义检测器 自定义文件类型检测器是旨在扩展默认检测功能的插件。 它们允许支持不常见的文件类型、非二进制格式或自定义检测行为。 检测器可以通过构造函数选项添加,也可以通过直接修改 `FileTypeParser#detectors` 添加。 通过构造函数提供的检测器在默认检测器之前执行。 检测器可以通过构造函数选项添加,也可以通过直接修改 `FileTypeParser#detectors` 添加。 ### 添加检测器示例 例如: ``` import {FileTypeParser} from 'file-type'; import {detectXml} from '@file-type/xml'; const parser = new FileTypeParser({customDetectors: [detectXml]}); const fileType = await parser.fromFile('sample.kml'); console.log(fileType); ``` ### 可用的第三方文件类型检测器 - [@file-type/av](https://github.com/Borewit/file-type-av):改进音频和视频文件格式的检测,并能准确区分两者 - [@file-type/cfbf](https://github.com/Borewit/file-type-cfbf):检测基于复合文件二进制格式 (CFBF) 的格式,例如 Office 97–2003 文档和 `.msi`。 - [@file-type/pdf](https://github.com/Borewit/file-type-pdf):检测基于 PDF 的文件类型,例如 Adobe Illustrator - [@file-type/xml](https://github.com/Borewit/file-type-xml):检测常见的 XML 文件类型,例如 GLM, KML, MusicXML, RSS, SVG, 和 XHTML ### 检测器执行流程 如果检测器返回 `undefined`,则适用以下规则: 1. **无 Tokenizer 交互**:如果检测器未修改 tokenizer 的位置,则执行序列中的下一个检测器。 2. **Tokenizer 交互**:如果检测器修改了 tokenizer 的位置(`tokenizer.position` 已前进),则不再执行后续检测器。在这种情况下,文件类型保持为 `undefined`,因为后续检测器无法评估内容。这是一种特殊情况,因为它会阻止任何其他检测器确定文件类型。 ### 编写您自己的自定义检测器 下面是一个自定义检测器数组的示例。这可以通过 `fileTypeOptions` 参数传递给 `FileTypeParser`。 ``` import {FileTypeParser} from 'file-type'; const unicornDetector = { id: 'unicorn', // May be used to recognize the detector in the detector list async detect(tokenizer) { const unicornHeader = [85, 78, 73, 67, 79, 82, 78]; // "UNICORN" in ASCII decimal const buffer = new Uint8Array(unicornHeader.length); await tokenizer.peekBuffer(buffer, {length: unicornHeader.length, mayBeLess: true}); if (unicornHeader.every((value, index) => value === buffer[index])) { return {ext: 'unicorn', mime: 'application/unicorn'}; } return undefined; } } const buffer = new Uint8Array([85, 78, 73, 67, 79, 82, 78]); const parser = new FileTypeParser({customDetectors: [unicornDetector]}); const fileType = await parser.fromBuffer(buffer); console.log(fileType); // {ext: 'unicorn', mime: 'application/unicorn'} ``` ``` /** @param tokenizer - The [tokenizer](https://github.com/Borewit/strtok3#tokenizer) used to read file content. @param fileType - The file type detected by standard or previous custom detectors, or `undefined` if no match is found. @returns The detected file type, or `undefined` if no match is found. */ export type Detector = (tokenizer: ITokenizer, fileType?: FileTypeResult) => Promise; ``` ## 中止信号 某些异步操作可以通过向 `FileTypeParser` 构造函数传递 [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) 来中止。 ``` import {FileTypeParser} from 'file-type'; const abortController = new AbortController() const parser = new FileTypeParser({abortSignal: abortController.signal}); const promise = parser.fromStream(blob.stream()); abortController.abort(); // Abort file-type reading from the Blob stream. ``` ## 支持的文件类型 - [`3g2`](https://en.wikipedia.org/wiki/3GP_and_3G2#3G2) - 由 3GPP2 定义的多媒体容器格式,用于 3G CDMA2000 多媒体服务 - [`3gp`](https://en.wikipedia.org/wiki/3GP_and_3G2#3GP) - 由第三代合作伙伴项目 (3GPP) 定义的多媒体容器格式,用于 3G UMTS 多媒体服务 - [`3mf`](https://en.wikipedia.org/wiki/3D_Manufacturing_Format) - 3D 制造格式 - [`7z`](https://en.wikipedia.org/wiki/7z) - 7-Zip 压缩包 - [`Z`](https://fileinfo.com/extension/z) - Unix 压缩文件 - [`aac`](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) - 高级音频编码 - [`ac3`](https://www.atsc.org/standard/a522012-digital-audio-compression-ac-3-e-ac-3-standard-12172012/) - ATSC A/52 音频文件 - [`ace`](https://en.wikipedia.org/wiki/ACE_(compressed_file_format)) - ACE 压缩包 - [`aif`](https://en.wikipedia.org/wiki/Audio_Interchange_File_Format) - 音频交换文件 - [`alias`](https://en.wikipedia.org/wiki/Alias_%28Mac_OS%29) - macOS 别名文件 - [`amr`](https://en.wikipedia.org/wiki/Adaptive_Multi-Rate_audio_codec) - 自适应多速率音频编解码器 - [`ape`](https://en.wikipedia.org/wiki/Monkey%27s_Audio) - Monkey's Audio - [`apk`](https://en.wikipedia.org/wiki/Apk_(file_format)) - Android 安装包格式 - [`apng`](https://en.wikipedia.org/wiki/APNG) - 动画便携式网络图形 - [`ar`](https://en.wikipedia.org/wiki/Ar_(Unix)) - 存档文件 - [`arj`](https://en.wikipedia.org/wiki/ARJ) - 存档文件 - [`arrow`](https://arrow.apache.org) - 用于数据表的列式格式 - [`arw`](https://en.wikipedia.org/wiki/Raw_image_format#ARW) - Sony Alpha Raw 图像文件 - [`asar`](https://github.com/electron/asar#format) - 主要用于封装 Electron 应用程序的存档格式 - [`asf`](https://en.wikipedia.org/wiki/Advanced_Systems_Format) - 高级系统格式 - [`avi`](https://en.wikipedia.org/wiki/Audio_Video_Interleave) - 音频视频交错文件 - [`avif`](https://en.wikipedia.org/wiki/AV1#AV1_Image_File_Format_(AVIF)) - AV1 图像文件格式 - [`avro`](https://en.wikipedia.org/wiki/Apache_Avro#Avro_Object_Container_File) - 由 Apache Avro 开发的对象容器文件 - [`blend`](https://wiki.blender.org/index.php/Dev:Source/Architecture/File_Format) - Blender 项目 - [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format) - 位图图像文件 - [`bpg`](https://bellard.org/bpg/) - 更好的便携式图形文件 - [`bz2`](https://en.wikipedia.org/wiki/Bzip2) - 存档文件 - [`cab`](https://en.wikipedia.org/wiki/Cabinet_(file_format)) - Cabinet 文件 - [`cfb`](https://en.wikipedia.org/wiki/Compound_File_Binary_Format) - 复合文件二进制格式 - [`chm`](https://en.wikipedia.org/wiki/Microsoft_Compiled_HTML_Help) - Microsoft 编译的 HTML 帮助 - [`class`](https://en.wikipedia.org/wiki/Java_class_file) - Java 类文件 - [`cpio`](https://en.wikipedia.org/wiki/Cpio) - Cpio 存档 - [`cr2`](https://fileinfo.com/extension/cr2) - 佳能 Raw 图像文件 (v2) - [`cr3`](https://fileinfo.com/extension/cr3) - 佳能 Raw 图像文件 (v3) - [`crx`](https://developer.chrome.com/extensions/crx) - Google Chrome 扩展 - [`cur`](https://en.wikipedia.org/wiki/ICO_(file_format)) - 图标文件 - [`dat`](https://en.wikipedia.org/wiki/Windows_Registry) - Windows 注册表配置单元文件 - [`dcm`](https://en.wikipedia.org/wiki/DICOM#Data_format) - DICOM 图像文件 - [`deb`](https://en.wikipedia.org/wiki/Deb_(file_format)) - Debian 软件包 - [`dmg`](https://en.wikipedia.org/wiki/Apple_Disk_Image) - Apple 磁盘映像 - [`dng`](https://en.wikipedia.org/wiki/Digital_Negative) - Adobe 数字负片图像文件 - [`docm`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions) - Microsoft Word 启用宏的文档 - [`docx`](https://en.wikipedia.org/wiki/Office_Open_XML) - Microsoft Word 文档 - [`dotm`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions) - Microsoft Word 启用宏的模板 - [`dotx`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions) - Microsoft Word 模板 - [`drc`]() - Google 的 Draco 3D 数据压缩 - [`dsf`](https://dsd-guide.com/sites/default/files/white-papers/DSFFileFormatSpec_E.pdf) - Sony DSD 流文件 (DSF) - [`dwg`](https://en.wikipedia.org/wiki/.dwg) - Autodesk CAD 文件 - [`elf`](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) - Unix 可执行和可链接格式 - [`eot`](https://en.wikipedia.org/wiki/Embedded_OpenType) - 嵌入式 OpenType 字体 - [`eps`](https://en.wikipedia.org/wiki/Encapsulated_PostScript) - 封装的 PostScript - [`epub`](https://en.wikipedia.org/wiki/EPUB) - 电子书文件 - [`exe`](https://en.wikipedia.org/wiki/.exe) - 可执行文件 - [`f4a`](https://en.wikipedia.org/wiki/Flash_Video) - Adobe Flash Player 使用的纯音频 ISO 基础媒体文件格式 - [`f4b`](https://en.wikipedia.org/wiki/Flash_Video) - Adobe Flash Player 使用的有声读物和播客 ISO 基础媒体文件格式 - [`f4p`](https://en.wikipedia.org/wiki/Flash_Video) - Adobe Flash Player 使用的受 Adobe Access DRM 保护的 ISO 基础媒体文件格式 - [`f4v`](https://en.wikipedia.org/wiki/Flash_Video) - Adobe Flash Player 使用的 ISO 基础媒体文件格式 - [`fbx`](https://en.wikipedia.org/wiki/FBX) - Filmbox 是一种专有文件格式,用于在数字内容创建应用程序之间提供互操作性。 - [`flac`](https://en.wikipedia.org/wiki/FLAC) - 无损音频编解码器 - [`flif`](https://en.wikipedia.org/wiki/Free_Lossless_Image_Format) - 免费无损图像格式 - [`flv`](https://en.wikipedia.org/wiki/Flash_Video) - Flash 视频 - [`gif`](https://en.wikipedia.org/wiki/GIF) - 图形交换格式 - [`glb`](https://github.com/KhronosGroup/glTF) - GL 传输格式 - [`gz`](https://en.wikipedia.org/wiki/Gzip) - 存档文件 - [`heic`](https://nokiatech.github.io/heif/technical.html) - 高效图像文件格式 - [`icc`](https://en.wikipedia.org/wiki/ICC_profile) - ICC 配置文件 - [`icns`](https://en.wikipedia.org/wiki/Apple_Icon_Image_format) - Apple 图标图像 - [`ico`](https://en.wikipedia.org/wiki/ICO_(file_format)) - Windows 图标文件 - [`ics`](https://en.wikipedia.org/wiki/ICalendar#Data_format) - iCalendar - [`indd`](https://en.wikipedia.org/wiki/Adobe_InDesign#File_format) - Adobe InDesign 文档 - [`it`](https://wiki.openmpt.org/Manual:_Module_formats#The_Impulse_Tracker_format_.28.it.29) - 音频模块格式:Impulse Tracker - [`j2c`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000 - [`jar`](https://en.wikipedia.org/wiki/JAR_(file_format)) - Java 存档 - [`jls`](https://en.wikipedia.org/wiki/Lossless_JPEG#JPEG-LS) - 连续色调图像的无损/近无损压缩标准 - [`jmp`](https://en.wikipedia.org/wiki/JMP_(statistical_software)) - SAS Institute 的 JMP 数据文件格式 - [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000 - [`jpg`](https://en.wikipedia.org/wiki/JPEG) - 联合图像专家组图像 - [`jpm`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000 - [`jpx`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000 - [`jxl`](https://en.wikipedia.org/wiki/JPEG_XL) - JPEG XL 图像格式 - [`jxr`](https://en.wikipedia.org/wiki/JPEG_XR) - 联合图像专家组扩展范围 - [`ktx`](https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/) - OpenGL 和 OpenGL ES 纹理 - [`lnk`](https://en.wikipedia.org/wiki/Shortcut_%28computing%29#Microsoft_Windows) - Microsoft Windows 文件快捷方式 - [`lz`](https://en.wikipedia.org/wiki/Lzip) - 存档文件 - [`lz4`](https://en.wikipedia.org/wiki/LZ4_(compression_algorithm)) - 由多种 LZ4 压缩实用程序之一创建的压缩存档 - [`lzh`](https://en.wikipedia.org/wiki/LHA_(file_format)) - LZH 存档 - [`m4a`](https://en.wikipedia.org/wiki/M4A) - 纯音频 MPEG-4 文件 - [`m4b`](https://en.wikipedia.org/wiki/M4B) - 有声读物和播客 MPEG-4 文件,也包含元数据,包括章节标记、图像和超链接 - [`m4p`](https://en.wikipedia.org/wiki/MPEG-4_Part_14#Filename_extensions) - 音频流受 FairPlay 数字版权管理加密的 MPEG-4 文件,通过 iTunes Store 出售 - [`m4v`](https://en.wikipedia.org/wiki/M4V) - Apple 开发的视频容器格式,与 MP4 格式非常相似 - [`macho`](https://en.wikipedia.org/wiki/Mach-O) - Mach-O 二进制格式 - [`mid`](https://en.wikipedia.org/wiki/MIDI) - 乐器数字接口文件 - [`mie`](https://en.wikipedia.org/wiki/Sidecar_file) - 专用元信息格式,支持存储二进制以及文本元信息 - [`mj2`](https://en.wikipedia.org/wiki/Motion_JPEG_2000) - 运动 JPEG 2000 - [`mkv`](https://en.wikipedia.org/wiki/Matroska) - Matroska 视频文件 - [`mobi`](https://en.wikipedia.org/wiki/Mobipocket) - Mobipocket - [`mov`](https://en.wikipedia.org/wiki/QuickTime_File_Format) - QuickTime 视频文件 - [`mp1`](https://en.wikipedia.org/wiki/MPEG-1_Audio_Layer_I) - MPEG-1 音频层 I - [`mp2`](https://en.wikipedia.org/wiki/MPEG-1_Audio_Layer_II) - MPEG-1 音频层 II - [`mp3`](https://en.wikipedia.org/wiki/MP3) - 音频文件 - [`mp4`](https://en.wikipedia.org/wiki/MPEG-4_Part_14#Filename_extensions) - MPEG-4 第 14 部分视频文件 - [`mpc`](https://en.wikipedia.org/wiki/Musepack) - Musepack (SV7 & SV8) - [`mpg`](https://en.wikipedia.org/wiki/MPEG-1) - MPEG-1 文件 - [`mts`](https://en.wikipedia.org/wiki/.m2ts) - MPEG-2 传输流,包括原始版本和蓝光光盘音频视频 (BDAV) 版本 - [`mxf`](https://en.wikipedia.org/wiki/Material_Exchange_Format) - 素材交换格式 - [`nef`](https://www.nikonusa.com/en/learn-and-explore/a/products-and-innovation/nikon-electronic-format-nef.html) - 尼康电子格式图像文件 - [`nes`](https://fileinfo.com/extension/nes) - 任天堂 NES ROM - [`odg`](https://en.wikipedia.org/wiki/OpenDocument) - 用于绘图的 OpenDocument - [`odp`](https://en.wikipedia.org/wiki/OpenDocument) - 用于演示文稿的 OpenDocument - [`ods`](https://en.wikipedia.org/wiki/OpenDocument) - 用于电子表格的 OpenDocument - [`odt`](https://en.wikipedia.org/wiki/OpenDocument) - 用于文字处理的 OpenDocument - [`oga`](https://en.wikipedia.org/wiki/Ogg) - 音频文件 - [`ogg`](https://en.wikipedia.org/wiki/Ogg) - 音频文件 - [`ogm`](https://en.wikipedia.org/wiki/Ogg) - 音频文件 - [`ogv`](https://en.wikipedia.org/wiki/Ogg) - 音频文件 - [`ogx`](https://en.wikipedia.org/wiki/Ogg) - 音频文件 - [`opus`](https://en.wikipedia.org/wiki/Opus_(audio_format)) - 音频文件 - [`orf`](https://en.wikipedia.org/wiki/ORF_format) - 奥林巴斯 Raw 图像文件 - [`otf`](https://en.wikipedia.org/wiki/OpenType) - OpenType 字体 - [`otg`](https://en.wikipedia.org/wiki/OpenDocument_technical_specification#Templates) - 用于绘图的 OpenDocument 模板 - [`otp`](https://en.wikipedia.org/wiki/OpenDocument_technical_specification#Templates) - 用于演示文稿的 OpenDocument 模板 - [`ots`](https://en.wikipedia.org/wiki/OpenDocument_technical_specification#Templates) - 用于电子表格的 OpenDocument 模板 - [`ott`](https://en.wikipedia.org/wiki/OpenDocument_technical_specification#Templates) - 用于文字处理的 OpenDocument 模板 - [`parquet`](https://en.wikipedia.org/wiki/Apache_Parquet) - Apache Parquet - [`pcap`](https://wiki.wireshark.org/Development/LibpcapFileFormat) - Libpcap 文件格式 - [`pdf`](https://en.wikipedia.org/wiki/Portable_Document_Format) - 便携式文档格式 - [`pgp`](https://en.wikipedia.org/wiki/Pretty_Good_Privacy) - 优良保密协议 - [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics) - 便携式网络图形 - [`potm`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions) - Microsoft PowerPoint 启用宏的模板 - [`potx`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions) - Microsoft PowerPoint 模板 - [`ppsm`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions#PowerPoint) - Office PowerPoint 2007 启用宏的幻灯片放映 - [`ppsx`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions#PowerPoint) - Office PowerPoint 2007 幻灯片放映 - [`pptm`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions) - Microsoft PowerPoint 启用宏的文档 - [`pptx`](https://en.wikipedia.org/wiki/Office_Open_XML) - Microsoft PowerPoint 文档 - [`ps`](https://en.wikipedia.org/wiki/Postscript) - Postscript - [`psd`](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format) - Adobe Photoshop 文档 - [`pst`](https://en.wikipedia.org/wiki/Personal_Storage_Table) - 个人存储表文件 - [`qcp`](https://en.wikipedia.org/wiki/QCP) - 标记和分块数据 - [`raf`](https://en.wikipedia.org/wiki/Raw_image_format) - 富士 RAW 图像文件 - [`rar`](https://en.wikipedia.org/wiki/RAR_(file_format)) - 存档文件 - [`reg`](https://en.wikipedia.org/wiki/Windows_Registry) - Windows 注册表(条目)文件格式 - [`rm`](https://en.wikipedia.org/wiki/RealMedia) - RealMedia - [`rpm`](https://fileinfo.com/extension/rpm) - Red Hat 软件包管理器文件 - [`rtf`](https://en.wikipedia.org/wiki/Rich_Text_Format) - 富文本格式 - [`rw2`](https://en.wikipedia.org/wiki/Raw_image_format) - 松下 RAW 图像文件 - [`s3m`](https://wiki.openmpt.org/Manual:_Module_formats#The_ScreamTracker_3_format_.28.s3m.29) - 音频模块格式:ScreamTracker 3 - [`sav`](https://en.wikipedia.org/wiki/SPSS) - SPSS 统计数据文件 - [`shp`](https://en.wikipedia.org/wiki/Shapefile) - 地理空间矢量数据格式 - [`skp`](https://en.wikipedia.org/wiki/SketchUp) - SketchUp - [`spx`](https://en.wikipedia.org/wiki/Ogg) - 音频文件 - [`sqlite`](https://www.sqlite.org/fileformat2.html) - SQLite 文件 - [`stl`](https://en.wikipedia.org/wiki/STL_(file_format)) - 标准细分几何文件格式(仅 ASCII) - [`swf`](https://en.wikipedia.org/wiki/SWF) - Adobe Flash Player 文件 - [`tar`](https://en.wikipedia.org/wiki/Tar_(computing)#File_format) - Tape 归档或 tarball - [`tar.gz`](https://en.wikipedia.org/wiki/Gzip) - Gzip 压缩的 tape 归档 - [`tif`](https://en.wikipedia.org/wiki/Tagged_Image_File_Format) - 标记图像文件 - [`ttc`](https://en.wikipedia.org/wiki/TrueType#TrueType_Collection) - TrueType 集合字体 - [`ttf`](https://en.wikipedia.org/wiki/TrueType) - TrueType 字体 - [`vcf`](https://en.wikipedia.org/wiki/VCard) - vCard - [`voc`](https://wiki.multimedia.cx/index.php/Creative_Voice) - Creative Voice 文件 - [`vsdx`](https://en.wikipedia.org/wiki/Microsoft_Visio) - Microsoft Visio 文件 - [`vtt`](https://en.wikipedia.org/wiki/WebVTT) - WebVTT 文件(用于视频字幕) - [`wasm`](https://en.wikipedia.org/wiki/WebAssembly) - WebAssembly 中间编译格式 - [`wav`](https://en.wikipedia.org/wiki/WAV) - 波形音频文件 - [`webm`](https://en.wikipedia.org/wiki/WebM) - Web 视频文件 - [`webp`](https://en.wikipedia.org/wiki/WebP) - Web 图片格式 - [`woff`](https://en.wikipedia.org/wiki/Web_Open_Font_Format) - Web 开放字体格式 - [`woff2`](https://en.wikipedia.org/wiki/Web_Open_Font_Format) - Web 开放字体格式 - [`wv`](https://en.wikipedia.org/wiki/WavPack) - WavPack - [`xcf`](https://en.wikipedia.org/wiki/XCF_(file_format)) - 实验计算设施 - [`xlsm`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions) - Microsoft Excel 启用宏的文档 - [`xlsx`](https://en.wikipedia.org/wiki/Office_Open_XML) - Microsoft Excel 文档 - [`xltm`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions) - Microsoft Excel 启用宏的模板 - [`xltx`](https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions) - Microsoft Excel 模板 - [`xm`](https://wiki.openmpt.org/Manual:_Module_formats#The_FastTracker_2_format_.28.xm.29) - 音频模块格式:FastTracker 2 - [`xml`](https://en.wikipedia.org/wiki/XML) - 可扩展标记语言 - [`xpi`](https://en.wikipedia.org/wiki/XPInstall) - XPInstall 文件 - [`xz`](https://en.wikipedia.org/wiki/Xz) - 压缩文件 - [`zip`](https://en.wikipedia.org/wiki/Zip_(file_format)) - 存档文件 - [`zst`](https://en.wikipedia.org/wiki/Zstandard) - 存档文件 *欢迎针对其他常用文件类型提交 [Pull requests](.github/pull_request_template.md)。* 以下文件类型将不被接受,但其中大多数受[第三方检测器](#available-third-party-file-type-detectors)支持 - [MS-CFB:基于 Microsoft 复合文件二进制文件格式的格式](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cfb/53989ce4-7b05-4f8d-829b-d08d6148375b) - `.doc` - Microsoft Word 97-2003 文档 - `.xls` - Microsoft Excel 97-2003 文档 - `.ppt` - Microsoft PowerPoint97-2003 文档 - `.msi` - Microsoft Windows 安装程序 - `.csv` - [原因。](https://github.com/sindresorhus/file-type/issues/264#issuecomment-568439196) - `.svg` #### tokenizer 类型:[`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) 可用作被检查文件的源。 #### fileType 类型:`FileTypeResult` 具有 `ext`(扩展名)和 `mime`(mime 类型)属性的对象。 由标准检测或先前的自定义检测检测到的。如果找不到匹配的 fileTypeResult,则为 Undefined。 ## 相关 - [file-type-cli](https://github.com/sindresorhus/file-type-cli) - 此模块的 CLI - [image-dimensions](https://github.com/sindresorhus/image-dimensions) - 获取图像的尺寸 ## 维护者 - [Sindre Sorhus](https://github.com/sindresorhus) - [Borewit](https://github.com/Borewit)
标签:Buffer 处理, ESM 模块, GNU通用公共许可证, JavaScript 库, Magic Number, MIME 类型, MITM代理, Node.js, npm 包, TypeScript, URL发现, 二进制文件, 十六进制特征, 安全插件, 安全检测, 数据可视化, 文件上传校验, 文件格式识别, 文件签名, 文件类型检测, 文件解析, 流处理, 自定义脚本, 自定义脚本, 自定义脚本