本项目是 Debian apt 目录中 file 工具的 Windows CLI 版本,使用现代 C++20 技术重写。该工具可以检测文件的类型,支持多种文件格式的识别,包括文本文件、可执行文件、压缩文件、图像文件、音频文件、视频文件和文档文件等。
- 现代 C++20 实现:使用最新的 C++ 标准和特性,如
std::filesystem、std::optional、std::regex等 - 模块化设计:分离命令行解析、文件类型检测和魔术文件处理等功能
- 丰富的文件类型检测:
- 文本文件和二进制文件
- Windows PE32 可执行文件
- 压缩文件(ZIP、RAR、TAR)
- 图像文件(JPEG、PNG、GIF、BMP)
- 音频文件(MP3、WAV、FLAC)
- 视频文件(MP4、AVI)
- 文档文件(PDF、Microsoft Office)
- 魔术文件支持:通过解析魔术文件实现更精确的文件类型检测
- 命令行选项:支持与原 file 工具相似的命令行选项
- Windows 10 或更高版本
- Visual Studio 2022(Community、Professional 或 Enterprise 版)
- C++20 编译器支持(MSVC 14.30+)
- 打开项目:使用 Visual Studio 2022 打开
cpp20\file.sln解决方案文件 - 配置构建:
- 选择目标平台(x86 或 x64)
- 选择构建配置(Debug 或 Release)
- 编译项目:
- 点击 "生成" -> "生成解决方案" 或按 F7
- 编译成功后,可执行文件将生成在
cpp20\Release\或cpp20\x64\Release\目录中
Usage: file [OPTION...] [FILE...]
Determine type of FILEs.
-b, --brief brief output format
-c, --check check magic files for validity
-C, --compile compile magic files into binary format
-d, --debug print debugging information
-E, --error treat errors as fatal
-e, --exclude=TEST exclude TEST from checks
-f, --name-file=FILE read names from FILE
-F, --separator=STRING use STRING as separator
-i, --mime output MIME type
-k, --continue continue after first match
-l, --list list magic tests
-m, --magic-file=FILE use FILE as magic file
-n, --no-buffer do not buffer output
-N, --no-pad do not pad output
-p, --preserve-atime preserve access time
-P, --parameter=NAME=VALUE set parameter
-r, --raw do not convert unprintable chars
-s, --devices look at device contents
-S, --no-sandbox disable sandbox
-t, --no-test disable built-in tests
-v, --version print version information
-x, --exclude-quiet exclude tests quietly
-z, --compress check compressed files
-Z, --uncompress check compressed files transparently
-0, --print0 print null-separated output
--apple output Apple creator/type
--extension output file extensions
--mime-encoding output MIME encoding
--mime-type output MIME type
--help display this help and exit
# 检测单个文件
file.exe example.txt
# 检测多个文件
file.exe file1.exe file2.jpg file3.pdf
# 使用简要输出格式
file.exe -b example.txt
# 输出 MIME 类型
file.exe -i example.txt# 文本文件
example.txt: text/plain
# Windows 可执行文件
file.exe: PE32 executable (Windows)
# 图像文件
example.jpg: JPEG image
# 压缩文件
example.zip: ZIP archive
cpp20/
├── src/ # 源代码目录
│ ├── main.cpp # 主入口文件
│ ├── cli_parser.cpp # 命令行参数解析
│ ├── cli_parser.h # 命令行参数解析头文件
│ ├── file_detector.cpp # 文件类型检测实现
│ ├── file_detector.h # 文件类型检测头文件
│ ├── magic_parser.cpp # 魔术文件解析实现
│ └── magic_parser.h # 魔术文件解析头文件
├── magic/ # 魔术文件目录
│ ├── Header # 魔术文件头
│ ├── Localstuff # 本地魔术规则
│ └── Magdir/ # 按类别分类的魔术文件
├── Release/ # 发布版本输出目录
├── file.sln # Visual Studio 解决方案文件
├── file.vcxproj # Visual Studio 项目文件
└── README.md # 本说明文件
本项目使用魔术文件(magic files)来增强文件类型检测的准确性。魔术文件包含了各种文件格式的特征规则,用于识别不同类型的文件。
- 魔术文件位于
magic/目录下 - 默认情况下,工具会尝试加载
magic/Header文件 - 可以使用
-m选项指定自定义的魔术文件
- Visual Studio 2022:需要支持 C++20 标准的编译器
- Windows SDK:推荐使用 Windows 10 SDK 或更高版本
- 魔术文件:用于文件类型检测的规则文件(已包含在项目中)
-
魔术文件未找到
- 确保
magic/目录存在于可执行文件的同一目录或父目录中 - 可以使用
-m选项指定魔术文件的完整路径
- 确保
-
文件类型检测不准确
- 尝试使用不同的魔术文件
- 对于特殊文件格式,可能需要更新魔术文件规则
-
编译错误
- 确保使用 Visual Studio 2022 或更高版本
- 确保项目配置为使用 C++20 标准
- 检查是否安装了正确版本的 Windows SDK
本项目基于 Debian apt 中的 file 工具,使用原项目的许可证。有关详细信息,请参阅原始项目的 COPYING 文件。
- 原始 file 工具项目:https://git.in-ulm.de/cbiedl/file.git
- C++20 标准库文档
- Visual Studio 文档
- 当前版本:5.46 (C++20 Implementation)
- 构建系统:Visual Studio 2022
- C++ 标准:C++20