如何判断文件类型? [英] How to determine file type?

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

问题描述

我需要知道,如果我的文件是音频文件: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天全站免登陆