简体   繁体   English

跨平台方式获取用户友好的文件类型描述

[英]Cross-platform way to get user friendly file type description

What's a cross-platform way for getting a user-friendly description of a file? 获取用户友好的文件描述的跨平台方式是什么?

Examples: 例子:

  1. foo.pdf -> "Portable Document Format (PDF) foo.pdf - >“可移植文档格式(PDF)
  2. bar.doc -> "Microsoft Word Document" bar.doc - >“Microsoft Word文档”

Pointers to libraries or appropriate system APIs would be highly appreciated. 指向图书馆或适当的系统API的指针将受到高度赞赏。

A Qt/C++ solution is preferred but anything is fine. Qt / C ++解决方案是首选,但一切都很好。

Target platforms are Windows and Mac OS X. I'd prefer the descriptions to match what would be found in Explorer or Finder if possible (rather than maintaining a map of extensions -> descriptions myself). 目标平台是Windows和Mac OS X.如果可能的话,我更喜欢这些描述与资源管理器或Finder中的内容相匹配(而不是自己维护扩展地图 - >描述)。

The closest that I think you can get out of Qt is QFileInfo . 我认为你可以从Qt中获得的最接近的是QFileInfo

Windows keeps track of the mapping through the registry that can be accessed through Qt's QSettings . Windows通过注册表跟踪可通过Qt的QSettings访问的映射 But just from brief research it sounds like it might be kind tricky to mimic Explorer's mapping. 但仅仅通过简短的研究 ,听起来似乎模仿Explorer的映射可能有点棘手。

You can also launch the file with the default handler using QDesktopServices::openUrl() . 您还可以使用QDesktopServices :: openUrl()使用默认处理程序启动该文件。

I haven't researched how or where OSX keeps track of the file type description information. 我还没有研究过OSX如何或在哪里跟踪文件类型描述信息。

Hope that helps. 希望有所帮助。

The GNU File command is builtin for Linux and OSX, and there is a version available for Windows ( http://gnuwin32.sourceforge.net/packages/file.htm ). GNU File命令内置于Linux和OSX,并且有一个适用于Windows的版本( http://gnuwin32.sourceforge.net/packages/file.htm )。

File tests each argument in an attempt to classify it. 文件测试每个参数以尝试对其进行分类。 There are three sets of tests, performed in this order: filesystem tests, magic number tests, and language tests. 按顺序执行三组测试:文件系统测试,幻数测试和语言测试。 The first test that succeeds causes the file type to be printed. 成功的第一个测试会导致打印文件类型。 The type printed will usually contain one of the words text (the file contains only printing characters and a few common control characters and is probably safe to read on an ASCII terminal), executable (the file contains the result of compiling a program in a form understandable to some UNIX kernel or another), or data meaning anything else (data is usually `binary' or non-printable). 打印的类型通常包含一个单词text(该文件只包含打印字符和一些常用控制字符,并且可以安全地在ASCII终端上读取),可执行文件(该文件包含在表单中编译程序的结果)某些UNIX内核或其他内容可以理解,或者数据意味着其他任何东西(数据通常是“二进制”或不可打印)。 Exceptions are well-known file formats (core files, tar archives) that are known to contain binary data. 例外是已知包含二进制数据的众所周知的文件格式(核心文件,tar档案)。

You could invoke the file command using QProcess and display the returned info. 您可以使用QProcess调用file命令并显示返回的信息。

Output looks like : 输出如下:

$ file document.pdf
document.pdf: PDF document, version 1.5
$ file test.txt
test.txt: ASCII text, with CRLF, CR, LF line terminators

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM