通过文件名来判断有些是不准确的,最好的方式是通过文件内容来判断,通过文件内容判断有两种方法,一种文件的特征码,另一种是文件格式头,这里我只介绍通过文件格式头来判断的方法,思路来源之前看mplayer的源代码时,发现它是通过解析文件内容来判断,不过这部分代码主要是针对视频文件格式的,后来工作中用到需要准确判断文件格式的功能,就把这部分代码抽离出来,另外在此基础上增加了判断其他一些比较常见的WEB文件格式的代码,这部分功能都已经封装成了类,针对PHP来说可以把这个代码封装成PHP的插件,测试过效率还可以。

//***************************************************************************************

//

// Filename: WinType.h

//

// Description: CWinType 判断文件格式

//

// Version: 1.0

// Created: 2008-8-19

// Revision: none

//

// Author: 陈彦旭 (yanxu.chen@gmail.com)

//****************************************************************************************

/* *

类型描述 * 类型

"MS Windows special zipped file", * "zip"

"Icon for MS Windows", * "ico"

"HTML document text", * "htm"

"Zip archive data", * "zip"

"RAR archive data", * "rar"

"PDF document", * "pdf"

"Rich Text Format", * "doc"

"MPEG sequence", * "mpg"

"Microsoft ASF", * "wma"

"RealAudio sound file", * "rm"

"RealMedia file", * "rm"

"RIFF (little-endian) data, AVI", * "avi"

"RIFF (little-endian) data, wrapped MPEG-1 (CDXA)", * "dat",

"RIFF (little-endian) data, WAVE audio", * "wav"

"PNG image data", * "png"

"GIF image data", * "gif"

"JPEG image data", * "jpg"

"MS Windows icon resource", * "ico"

"Audio file with ID3", * "mp3"

"Macromedia Flash data", * "swf"

"Macromedia Flash Video", * "flv"

"JPEG 2000 image", * "jpg"

"MPEG ADTS, layer", * "mp3"

"MPEG ADTS, AAC", * "aac"

"JPEG image data", * "jpg"

"MS-DOS executable PE", * "exe","dll"

"QuickTime", * "mov"

"MPEG v4", * "mov"

*/

//******************************************************************************

#ifndef __WINTYPE_H__

#define __WINTYPE_H__

#include "public.h"

class CItem;

class CWinType

{

protected:

CWinType();

public:

virtual ~CWinType();

public:

BOOL Init();

BOOL Unit();

BOOL DelAllItem();

public:

wstring GetFileType(const char* pBuf, int nLen);

static CWinType* Instance();

static BOOL UnInstance();

protected:

BOOL RegsitAllType();

void AddItem(CItem* pItem);

private:

static CWinType* m_pInstance;

list m_List;

};

#endif

// WinType.cpp: implementation of the CWinType class.

//

//

#include "stdafx.h"

#include "WinType.h"

#include "Item.h"

CWinType* CWinType::m_pInstance = NULL;

CWinType* CWinType::Instance()

{

if (NULL == m_pInstance)

{

m_pInstance = new CWinType();

}

return m_pInstance;

}

BOOL CWinType::UnInstance()

{

if (NULL != m_pInstance)

{

delete m_pInstance;

}

m_pInstance = NULL;

return TRUE;

}

CWinType::CWinType()

{

}

CWinType::~CWinType()

{

Unit();

}

BOOL CWinType::Init()

{

return RegsitAllType();

}

BOOL CWinType::Unit()

{

DelAllItem();

return TRUE;

}

BOOL CWinType::DelAllItem()

{

list::iterator iter;

CItem* pItem = NULL;

for (iter=m_List.begin(); iter!=m_List.end(); ++iter)

{

pItem = *iter;

if (NULL != pItem)

{

delete pItem;

}

pItem = NULL;

}

m_List.clear();

return TRUE;

}

BOOL CWinType::RegsitAllType()

{

CItem* pItem = NULL;

pItem = new CAacType();

AddItem(pItem);

pItem = new CAsfType();

AddItem(pItem);

pItem = new CAVIType();

AddItem(pItem);

pItem = new CDatType();

AddItem(pItem);

pItem = new CFlvType();

AddItem(pItem);

pItem = new CMidType();

AddItem(pItem);

pItem = new CMovType();

AddItem(pItem);

pItem = new CRaType();

AddItem(pItem);

pItem = new CRmType();

AddItem(pItem);

pItem = new CWavType();

AddItem(pItem);

pItem = new CRarType();

AddItem(pItem);

pItem = new CZipType();

AddItem(pItem);

pItem = new CPdfType();

AddItem(pItem);

pItem = new CJpgType();

AddItem(pItem);

pItem = new CPngType();

AddItem(pItem);

pItem = new CGifType();

AddItem(pItem);

pItem = new CHtmType();

AddItem(pItem);

pItem = new CSwfType();

AddItem(pItem);

pItem = new CExeDllType();

AddItem(pItem);

pItem = new CMp3Type();

AddItem(pItem);

return TRUE;

}

void CWinType::AddItem(CItem* pItem)

{

this->m_List.push_back(pItem);

}

wstring CWinType::GetFileType(const char* pBuf, int nLen)

{

list::iterator iter;

CItem* pItem = NULL;

for (iter=m_List.begin(); iter!=m_List.end(); ++iter)

{

pItem = *iter;

if (NULL != pItem)

{

if (pItem->GetWinType((char*)pBuf, nLen))

{

return pItem->GetTypeDes();

}

}

pItem = NULL;

}

return L"";

}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐