public int GetFileType(string filePath)
{
int result = 0;
if (File.Exists(filePath) == false)
{
return 0;
}
try
{
FileStream fs = new FileStream(filePath, FileMode.Open);
int byteA = fs.ReadByte(); //byte[0]
int byteB = fs.ReadByte(); //byte[1]
fs.ReadByte(); //byte[2]
int byteC = fs.ReadByte(); //byte[3]
int byteD = fs.ReadByte(); //byte[4]
fs.Close();
fs.Dispose();
//ZIP:1
if (byteA == 80 && byteB == 75)
{
result = 1;
}
//LZH:2
else if (byteC == 108 && byteD == 104)
{
result = 2;
}
//TGZ:3
else if (byteA == 31 && byteB == 139)
{
result = 3;
}
//UNKNOWN:0
else
{
result = 0;
}
}
catch
{
return 0;
}
return result;
}
压缩文件格式判定
最新推荐文章于 2022-12-02 15:24:36 发布