|
| 1 | +# File Tool for Windows (C++20 Implementation) |
| 2 | + |
| 3 | +## 项目概述 |
| 4 | + |
| 5 | +本项目是 Debian apt 目录中 file 工具的 Windows CLI 版本,使用现代 C++20 技术重写。该工具可以检测文件的类型,支持多种文件格式的识别,包括文本文件、可执行文件、压缩文件、图像文件、音频文件、视频文件和文档文件等。 |
| 6 | + |
| 7 | +## 功能特性 |
| 8 | + |
| 9 | +- **现代 C++20 实现**:使用最新的 C++ 标准和特性,如 `std::filesystem`、`std::optional`、`std::regex` 等 |
| 10 | +- **模块化设计**:分离命令行解析、文件类型检测和魔术文件处理等功能 |
| 11 | +- **丰富的文件类型检测**: |
| 12 | + - 文本文件和二进制文件 |
| 13 | + - Windows PE32 可执行文件 |
| 14 | + - 压缩文件(ZIP、RAR、TAR) |
| 15 | + - 图像文件(JPEG、PNG、GIF、BMP) |
| 16 | + - 音频文件(MP3、WAV、FLAC) |
| 17 | + - 视频文件(MP4、AVI) |
| 18 | + - 文档文件(PDF、Microsoft Office) |
| 19 | +- **魔术文件支持**:通过解析魔术文件实现更精确的文件类型检测 |
| 20 | +- **命令行选项**:支持与原 file 工具相似的命令行选项 |
| 21 | + |
| 22 | +## 系统要求 |
| 23 | + |
| 24 | +- Windows 10 或更高版本 |
| 25 | +- Visual Studio 2022(Community、Professional 或 Enterprise 版) |
| 26 | +- C++20 编译器支持(MSVC 14.30+) |
| 27 | + |
| 28 | +## 构建说明 |
| 29 | + |
| 30 | +1. **打开项目**:使用 Visual Studio 2022 打开 `cpp20\file.sln` 解决方案文件 |
| 31 | +2. **配置构建**: |
| 32 | + - 选择目标平台(x86 或 x64) |
| 33 | + - 选择构建配置(Debug 或 Release) |
| 34 | +3. **编译项目**: |
| 35 | + - 点击 "生成" -> "生成解决方案" 或按 F7 |
| 36 | + - 编译成功后,可执行文件将生成在 `cpp20\Release\` 或 `cpp20\x64\Release\` 目录中 |
| 37 | + |
| 38 | +## 命令行选项 |
| 39 | + |
| 40 | +``` |
| 41 | +Usage: file [OPTION...] [FILE...] |
| 42 | +Determine type of FILEs. |
| 43 | +
|
| 44 | + -b, --brief brief output format |
| 45 | + -c, --check check magic files for validity |
| 46 | + -C, --compile compile magic files into binary format |
| 47 | + -d, --debug print debugging information |
| 48 | + -E, --error treat errors as fatal |
| 49 | + -e, --exclude=TEST exclude TEST from checks |
| 50 | + -f, --name-file=FILE read names from FILE |
| 51 | + -F, --separator=STRING use STRING as separator |
| 52 | + -i, --mime output MIME type |
| 53 | + -k, --continue continue after first match |
| 54 | + -l, --list list magic tests |
| 55 | + -m, --magic-file=FILE use FILE as magic file |
| 56 | + -n, --no-buffer do not buffer output |
| 57 | + -N, --no-pad do not pad output |
| 58 | + -p, --preserve-atime preserve access time |
| 59 | + -P, --parameter=NAME=VALUE set parameter |
| 60 | + -r, --raw do not convert unprintable chars |
| 61 | + -s, --devices look at device contents |
| 62 | + -S, --no-sandbox disable sandbox |
| 63 | + -t, --no-test disable built-in tests |
| 64 | + -v, --version print version information |
| 65 | + -x, --exclude-quiet exclude tests quietly |
| 66 | + -z, --compress check compressed files |
| 67 | + -Z, --uncompress check compressed files transparently |
| 68 | + -0, --print0 print null-separated output |
| 69 | + --apple output Apple creator/type |
| 70 | + --extension output file extensions |
| 71 | + --mime-encoding output MIME encoding |
| 72 | + --mime-type output MIME type |
| 73 | + --help display this help and exit |
| 74 | +``` |
| 75 | + |
| 76 | +## 使用示例 |
| 77 | + |
| 78 | +### 基本用法 |
| 79 | + |
| 80 | +```bash |
| 81 | +# 检测单个文件 |
| 82 | +file.exe example.txt |
| 83 | + |
| 84 | +# 检测多个文件 |
| 85 | +file.exe file1.exe file2.jpg file3.pdf |
| 86 | + |
| 87 | +# 使用简要输出格式 |
| 88 | +file.exe -b example.txt |
| 89 | + |
| 90 | +# 输出 MIME 类型 |
| 91 | +file.exe -i example.txt |
| 92 | +``` |
| 93 | + |
| 94 | +### 示例输出 |
| 95 | + |
| 96 | +``` |
| 97 | +# 文本文件 |
| 98 | +example.txt: text/plain |
| 99 | +
|
| 100 | +# Windows 可执行文件 |
| 101 | +file.exe: PE32 executable (Windows) |
| 102 | +
|
| 103 | +# 图像文件 |
| 104 | +example.jpg: JPEG image |
| 105 | +
|
| 106 | +# 压缩文件 |
| 107 | +example.zip: ZIP archive |
| 108 | +``` |
| 109 | + |
| 110 | +## 项目结构 |
| 111 | + |
| 112 | +``` |
| 113 | +cpp20/ |
| 114 | +├── src/ # 源代码目录 |
| 115 | +│ ├── main.cpp # 主入口文件 |
| 116 | +│ ├── cli_parser.cpp # 命令行参数解析 |
| 117 | +│ ├── cli_parser.h # 命令行参数解析头文件 |
| 118 | +│ ├── file_detector.cpp # 文件类型检测实现 |
| 119 | +│ ├── file_detector.h # 文件类型检测头文件 |
| 120 | +│ ├── magic_parser.cpp # 魔术文件解析实现 |
| 121 | +│ └── magic_parser.h # 魔术文件解析头文件 |
| 122 | +├── magic/ # 魔术文件目录 |
| 123 | +│ ├── Header # 魔术文件头 |
| 124 | +│ ├── Localstuff # 本地魔术规则 |
| 125 | +│ └── Magdir/ # 按类别分类的魔术文件 |
| 126 | +├── Release/ # 发布版本输出目录 |
| 127 | +├── file.sln # Visual Studio 解决方案文件 |
| 128 | +├── file.vcxproj # Visual Studio 项目文件 |
| 129 | +└── README.md # 本说明文件 |
| 130 | +``` |
| 131 | + |
| 132 | +## 魔术文件 |
| 133 | + |
| 134 | +本项目使用魔术文件(magic files)来增强文件类型检测的准确性。魔术文件包含了各种文件格式的特征规则,用于识别不同类型的文件。 |
| 135 | + |
| 136 | +- 魔术文件位于 `magic/` 目录下 |
| 137 | +- 默认情况下,工具会尝试加载 `magic/Header` 文件 |
| 138 | +- 可以使用 `-m` 选项指定自定义的魔术文件 |
| 139 | + |
| 140 | +## 构建依赖 |
| 141 | + |
| 142 | +- **Visual Studio 2022**:需要支持 C++20 标准的编译器 |
| 143 | +- **Windows SDK**:推荐使用 Windows 10 SDK 或更高版本 |
| 144 | +- **魔术文件**:用于文件类型检测的规则文件(已包含在项目中) |
| 145 | + |
| 146 | +## 故障排除 |
| 147 | + |
| 148 | +### 常见问题 |
| 149 | + |
| 150 | +1. **魔术文件未找到** |
| 151 | + - 确保 `magic/` 目录存在于可执行文件的同一目录或父目录中 |
| 152 | + - 可以使用 `-m` 选项指定魔术文件的完整路径 |
| 153 | + |
| 154 | +2. **文件类型检测不准确** |
| 155 | + - 尝试使用不同的魔术文件 |
| 156 | + - 对于特殊文件格式,可能需要更新魔术文件规则 |
| 157 | + |
| 158 | +3. **编译错误** |
| 159 | + - 确保使用 Visual Studio 2022 或更高版本 |
| 160 | + - 确保项目配置为使用 C++20 标准 |
| 161 | + - 检查是否安装了正确版本的 Windows SDK |
| 162 | + |
| 163 | +## 许可证 |
| 164 | + |
| 165 | +本项目基于 Debian apt 中的 file 工具,使用原项目的许可证。有关详细信息,请参阅原始项目的 COPYING 文件。 |
| 166 | + |
| 167 | +## 致谢 |
| 168 | + |
| 169 | +- 原始 file 工具项目:https://git.in-ulm.de/cbiedl/file.git |
| 170 | +- C++20 标准库文档 |
| 171 | +- Visual Studio 文档 |
| 172 | + |
| 173 | +## 版本信息 |
| 174 | + |
| 175 | +- 当前版本:5.46 (C++20 Implementation) |
| 176 | +- 构建系统:Visual Studio 2022 |
| 177 | +- C++ 标准:C++20 |
0 commit comments