文件签名

用文件签名确定文件类型

判断文件类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string path = @"c:\7aec54e736d12f2e64c3075946c2d562843568c1.png";
FileStream filestream = new FileStream(path, FileMode.Open);

byte[] buffer = new byte[filestream.Length];

filestream.Read(buffer, 0, buffer.Length);
filestream.Close();

// string fileBase64 = Convert.ToBase64String(buffer);

string signature = "";
for (int i = 0; i < 4; i++)
{
signature += Convert.ToString(buffer[i], 16);
}

在获得了文件字节数组字符串之后可以通过比较文件签名来获取文件的真实类型

参考

File format
Magic number (programming)
List of file signatures

0%