簡體   English   中英

如何區分zip檔案中的xlsx和docx文件?

[英]How to distinguish xlsx and docx files from zip archives?

我有一個用例,其中我需要知道文件的文件類型,以識別可執行文件(exe,安裝程序等)和存檔文件(zip,rar等)並將其列入黑名單。 因此,依靠擴展名對我來說還不夠,因為可以更改文件的擴展名,但文件屬性將保持不變。 我嘗試使用linux命令:

file --b filename

上面的解決方案可以與.xlsx和.docx文件以外的所有文件類型完美配合,因為該命令為.xlsx和.docx提供了以下內容

Zip存檔數據,至少要提取v2.0

因此,我最終也將.xlsx和.docx文件列入黑名單。

有人可以建議我一種不使用適用於xlsx和docx的擴展名的文件類型的方法。

您必須更新file命令(或其魔術文件)。

最新版本可以識別MSOOXML文件:

$ file -b test.docx
Microsoft Word 2007+

$ file --version
file-5.32

我使用了Mimemagic 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM