如何确定文件类型? [英] How to determine file type?

查看:24
本文介绍了如何确定文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道我的文件是否是音频文件:mp3、wav 等...
如何做到这一点?

I need to know if my file is audio file: mp3, wav, etc...
How to do this?

推荐答案

嗯,最可靠的方法是为你想要检测的文件类型编写一个解析器,然后尝试——如果没有错误,很明显您尝试过的类型.然而,这是一种昂贵的方法,但它可以确保您也可以成功加载文件,因为它还会检查文件的其余部分的语义合理性.

Well, the most robust way would be to write a parser for the file types you want to detect and then just try – if there are no errors, it's obviously of the type you tried. This is an expensive approach, however, but it would ensure that you can successfully load the file as well since it will also check the rest of the file for semantic soundness.

一个更便宜的变体是寻找魔法"字节——文件开头的签名或已知的偏移量.例如,如果一个文件以 ID3 标签开头,您可以合理地确定它是一个 MP3 文件.如果一个文件以RIFF¼↕☻ WAVEfmt开头,那么它就是一个WAV文件.然而,这样的检测并不能保证文件确实是那种类型——它可能只是签名和垃圾.

A much less expensive variant would be to look for "magic" bytes – signatures at the start or known offsets of the file. For example, if a file starts with an ID3 tag you can be reasonably sure it's an MP3 file. If a file starts with RIFF¼↕☻ WAVEfmt, then it's a WAV file. However, such detection cannot guarantee you that the file is really of that type – it could just be the signature and following that garbage.

这篇关于如何确定文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆