C# - 搜索 Excel 和 Powerpoint 文件格式字节序列

问题描述

我正在管理服务器端不同类型文件上传。 我实现了一个操作,通过将文件byte sequence 与特定文件格式的 byte sequence 进行比较来处理返回文件格式。 在搜索 I found this answer 时,这对我帮助很大。 所以我这样实施我的行动:

private static MediaFormat GetFormat(byte[] bytes,string fileName = null)
{
    // these are my file formats byte sequences
    byte[] jpeg = new byte[] { 255,216,255,224 };
    byte[] jpeg2 = new byte[] { 255,225 };
    byte[] png = new byte[] { 137,80,78,71 };
    byte[] doc = new byte[] { 208,207,17,224,161,177,26,225 };
    byte[] docx_zip = new byte[] { 80,75,3,4 };
    byte[] pdf = new byte[] { 37,68,70,45,49,46 };

    if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
        return MediaFormat.jpg;
    if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
        return MediaFormat.jpg;
    if (png.SequenceEqual(bytes.Take(png.Length)))
        return MediaFormat.png;
    if (doc.SequenceEqual(bytes.Take(doc.Length)))
        return MediaFormat.doc;
    if (docx_zip.SequenceEqual(bytes.Take(docx_zip.Length)))
    {
        if (!string.IsNullOrEmpty(fileName) && fileName.Contains(".zip",StringComparison.OrdinalIgnoreCase))
            return MediaFormat.zip;

        return MediaFormat.docx;
    }
    if (pdf.SequenceEqual(bytes.Take(pdf.Length)))
        return MediaFormat.pdf;

    return MediaFormat.unkNown;
}

在我发现(并在这个问题中分享)的答案中,创建者指出了一个站点链接,我可以在其中找到其他字节序列来识别其他格式,但不幸的是该站点404 所以我不能找到我需要的所有格式,PowerPoint (.ppt,.pptx) 和 Excel 和 CSV (.xlx,.xlxs,.csv) 甚至 {{1 }} 是可能的。

谁能告诉我正确的字节序列是什么或者我在哪里可以找到它们? 非常感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)