windows客户端开发--获取windows系统中文件的MIME
Posted 江南-一苇渡江
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows客户端开发--获取windows系统中文件的MIME相关的知识,希望对你有一定的参考价值。
首先,什么是MIME,是不是文件的后缀名呢?
当然不是。
有时候我们需要获取某个文件的后缀名,这也许对你来说太小case了,你可能不加思考的写了一个函数,更加文件名字符串查找最后一个’.’,然后取最有一个’.’之后的字符串,即为我们要得到的后缀名。
看似非常完美,但确实漏洞百出。
如果我的文件没有后缀名怎么办?
如果我的一张png图片,我强制把后缀名改为jpg怎么办?
这样你根据文件名字方法就不能获得百分百正确的后缀名。
所以,就该MIME出场了!
何为MIME?
MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准。
MIME 消息能包含文本、图像、音频、视频以及其他应用程序专用的数据。
接下来的工作就是在windows系统上,如何根据一个文件名路径,获取这个文件的mime呢?
起初,我觉得应该找一个第三方库来实现这个小小的功能,比如libmagic。
但是暮然回首,发现windows为我们提供了api。这里要插一句:windows确实是最复杂的操作系统。有时候我们再忽略了太多windows api给我们提供的函数。
FindMimeFromData
作用:
Determines the MIME type from the data provided.
原型:
HRESULT FindMimeFromData(
LPBC pBC,
LPCWSTR pwzUrl,
LPVOID pBuffer,
DWORD cbSize,
LPCWSTR pwzMimeProposed,
DWORD dwMimeFlags,
LPWSTR *ppwzMimeOut,
_Reserved_ DWORD dwReserved
);
参数:
pBC
A pointer to the IBindCtx interface. Can be set to NULL.
pwzUrl
A pointer to a string value that contains the URL of the data. Can be set to NULL if pBuffer contains the data to be sniffed.
pBuffer
A pointer to the buffer that contains the data to be sniffed. Can be set to NULL if pwzUrl contains a valid URL.
cbSize
An unsigned long integer value that contains the size of the buffer.
pwzMimeProposed
A pointer to a string value that contains the proposed MIME type. This value is authoritative if type cannot be determined from the data. If the proposed type contains a semi-colon (;) it is removed. This parameter can be set to NULL.
下面看看如何应用,首先是读一个文件,然后调用FindMimeFromData函数即可:
#include <urlmon.h>
#include <stdio.h>
#include <Windows.h>
#include <iostream>
#pragma comment(lib, "Urlmon.lib")
int main(int argc, char* argv[])
{
char buff[256];
LPWSTR out;
FILE *in = fopen("D:\\libsakura.lib", "rb");
fread(buff, 1, 256, in);
FindMimeFromData(NULL, NULL, buff, 256, NULL, FMFD_DEFAULT, &out, 0);
printf("%ls\n", out);
return 0;
}
下面是文件类型对应的MIME,仅供参考:
application/envoy evy
application/fractals fif
application/futuresplash spl
application/hta hta
application/internet-property-stream acx
application/mac-binhex40 hqx
application/msword doc
application/msword dot
application/octet-stream *
application/octet-stream bin
application/octet-stream class
application/octet-stream dms
application/octet-stream exe
application/octet-stream lha
application/octet-stream lzh
application/oda oda
application/olescript axs
application/pdf pdf
application/pics-rules prf
application/pkcs10 p10
application/pkix-crl crl
application/postscript ai
application/postscript eps
application/postscript ps
application/rtf rtf
application/set-payment-initiation setpay
application/set-registration-initiation setreg
application/vnd.ms-excel xla
application/vnd.ms-excel xlc
application/vnd.ms-excel xlm
application/vnd.ms-excel xls
application/vnd.ms-excel xlt
application/vnd.ms-excel xlw
application/vnd.ms-outlook msg
application/vnd.ms-pkicertstore sst
application/vnd.ms-pkiseccat cat
application/vnd.ms-pkistl stl
application/vnd.ms-powerpoint pot
application/vnd.ms-powerpoint pps
application/vnd.ms-powerpoint ppt
application/vnd.ms-project mpp
application/vnd.ms-works wcm
application/vnd.ms-works wdb
application/vnd.ms-works wks
application/vnd.ms-works wps
application/winhlp hlp
application/x-bcpio bcpio
application/x-cdf cdf
application/x-compress z
application/x-compressed tgz
application/x-cpio cpio
application/x-csh csh
application/x-director dcr
application/x-director dir
application/x-director dxr
application/x-dvi dvi
application/x-gtar gtar
application/x-gzip gz
application/x-hdf hdf
application/x-internet-signup ins
application/x-internet-signup isp
application/x-iphone iii
application/x-javascript js
application/x-latex latex
application/x-msaccess mdb
application/x-mscardfile crd
application/x-msclip clp
application/x-msdownload dll
application/x-msmediaview m13
application/x-msmediaview m14
application/x-msmediaview mvb
application/x-msmetafile wmf
application/x-msmoney mny
application/x-mspublisher pub
application/x-msschedule scd
application/x-msterminal trm
application/x-mswrite wri
application/x-netcdf cdf
application/x-netcdf nc
application/x-perfmon pma
application/x-perfmon pmc
application/x-perfmon pml
application/x-perfmon pmr
application/x-perfmon pmw
application/x-pkcs12 p12
application/x-pkcs12 pfx
application/x-pkcs7-certificates p7b
application/x-pkcs7-certificates spc
application/x-pkcs7-certreqresp p7r
application/x-pkcs7-mime p7c
application/x-pkcs7-mime p7m
application/x-pkcs7-signature p7s
application/x-sh sh
application/x-shar shar
application/x-shockwave-flash swf
application/x-stuffit sit
application/x-sv4cpio sv4cpio
application/x-sv4crc sv4crc
application/x-tar tar
application/x-tcl tcl
application/x-tex tex
application/x-texinfo texi
application/x-texinfo texinfo
application/x-troff roff
application/x-troff t
application/x-troff tr
application/x-troff-man man
application/x-troff-me me
application/x-troff-ms ms
application/x-ustar ustar
application/x-wais-source src
application/x-x509-ca-cert cer
application/x-x509-ca-cert crt
application/x-x509-ca-cert der
application/ynd.ms-pkipko pko
application/zip zip
audio/basic au
audio/basic snd
audio/mid mid
audio/mid rmi
audio/mpeg mp3
audio/x-aiff aif
audio/x-aiff aifc
audio/x-aiff aiff
audio/x-mpegurl m3u
audio/x-pn-realaudio ra
audio/x-pn-realaudio ram
audio/x-wav wav
image/bmp bmp
image/cis-cod cod
image/gif gif
image/ief ief
image/jpeg jpe
image/jpeg jpeg
image/jpeg jpg
image/pipeg jfif
image/svg+xml svg
image/tiff tif
image/tiff tiff
image/x-cmu-raster ras
image/x-cmx cmx
image/x-icon ico
image/x-portable-anymap pnm
image/x-portable-bitmap pbm
image/x-portable-graymap pgm
image/x-portable-pixmap ppm
image/x-rgb rgb
image/x-xbitmap xbm
image/x-xpixmap xpm
image/x-xwindowdump xwd
message/rfc822 mht
message/rfc822 mhtml
message/rfc822 nws
text/css css
text/h323 323
text/html htm
text/html html
text/html stm
text/iuls uls
text/plain bas
text/plain c
text/plain h
text/plain txt
text/richtext rtx
text/scriptlet sct
text/tab-separated-values tsv
text/webviewhtml htt
text/x-component htc
text/x-setext etx
text/x-vcard vcf
video/mpeg mp2
video/mpeg mpa
video/mpeg mpe
video/mpeg mpeg
video/mpeg mpg
video/mpeg mpv2
video/quicktime mov
video/quicktime qt
video/x-la-asf lsf
video/x-la-asf lsx
video/x-ms-asf asf
video/x-ms-asf asr
video/x-ms-asf asx
video/x-msvideo avi
video/x-sgi-movie movie
x-world/x-vrml flr
x-world/x-vrml vrml
x-world/x-vrml wrl
x-world/x-vrml wrz
x-world/x-vrml xaf
x-world/x-vrml xof
以上是关于windows客户端开发--获取windows系统中文件的MIME的主要内容,如果未能解决你的问题,请参考以下文章