我有一个用例,我需要知道文件的文件类型,以识别和黑名单可执行文件(exe,安装程序等),归档文件(zip,rar等)。因此,依赖扩展名对我来说是不够的,因为文件的扩展名可以更改,但是文件属性将保持不变。我尝试使用linux命令:
file --b filename
上述解决方案与除.xlsx和.docx文件之外的所有文件类型都能很好地工作,因为该命令为.xlsx和.docx提供了以下命令
压缩归档数据,至少要提取v2.0
正因为如此,我最终也将.xlsx和.docx文件列入黑名单。
有人能建议我一种不使用xlsx和docx的扩展名就可以获得文件类型的方法吗?
发布于 2018-03-29 10:14:38
我使用米米卡宝石并添加了自定义魔术(Gem调用它)来标识xlsx、docx和pptx文件格式。而且,这不依赖于文件扩展名。
以下是我添加的魔法列表:
[['application/vnd.openxmlformats-officedocument.wordprocessingml.document.custom', [[0, "PK\x03\x04", [[30, '_rels/.rels', [[0..5000, 'word/']]]]]]],
['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.custom', [[0, "PK\003\004", [[30, '_rels/.rels', [[0..5000, 'xl/']]]]]]],
['application/vnd.openxmlformats-officedocument.presentationml.presentation.custom', [[0, "PK\003\004", [[30, '_rels/.rels', [[0..5000, 'ppt/']]]]]]],['application/vnd.openxmlformats-officedocument.wordprocessingml.document.custom', [[0, "PK\x03\x04", [[30, 'word/']]]]],
['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.custom', [[0, "PK\003\004", [[30, 'xl/']]]]],
['application/vnd.openxmlformats-officedocument.presentationml.presentation.custom', [[0, "PK\003\004", [[30, 'ppt/']]]]]].each do |magic|
MimeMagic.add(magic[0], magic: magic[1])
end
发布于 2018-01-10 15:52:30
您必须更新您的file
命令(或它的神奇文件)。
最近的版本确实识别MSOOXML文件:
$ file -b test.docx
Microsoft Word 2007+
$ file --version
file-5.32
https://stackoverflow.com/questions/48188346
复制相似问题