Original file bytes from StreamReader, magic number detection

限于喜欢 提交于 2020-03-18 12:15:37

问题


I'm trying to differentiate between "text files" and "binary" files, as I would effectively like to ignore files with "unreadable" contents.

I have a file that I believe is a GZIP archive. I'm tring to ignore this kind of file by detecting the magic numbers / file signature. If I open the file with the Hex editor plugin in Notepad++ I can see the first three hex codes are 1f 8b 08.

However if I read the file using a StreamReader, I'm not sure how to get to the original bytes..

using (var streamReader = new StreamReader(@"C:\file"))
{
    char[] buffer = new char[10];
    streamReader.Read(buffer, 0, 10);
    var s = new String(buffer);

    byte[] bytes = new byte[6];
    System.Buffer.BlockCopy(s.ToCharArray(), 0, bytes, 0, 6);
    var hex = BitConverter.ToString(bytes);

    var otherhex = BitConverter.ToString(System.Text.Encoding.UTF8.GetBytes(s.ToCharArray()));
}

At the end of the using statement I have the following variable values:

hex: "1F-00-FD-FF-08-00"
otherhex: "1F-EF-BF-BD-08-00-EF-BF-BD-EF-BF-BD-0A-51-02-03"

Neither of which start with the hex values shown in Notepad++.

Is it possible to get the original bytes from the result of reading a file via StreamReader?


回答1:


Your code tries to change a binary buffer into a string. Strings are Unicode in NET so two bytes are required. The resulting is a bit unpredictable as you can see.

Just use a BinaryReader and its ReadBytes method

using(FileStream fs = new FileStream(@"C:\file", FileMode.Open, FileAccess.Read))
{
    using (var reader = new BinaryReader(fs, new ASCIIEncoding()))
    {
        byte[] buffer = new byte[10];
        buffer = reader.ReadBytes(10);
        if(buffer[0] == 31 && buffer[1] == 139 && buffer[2] == 8)
            // you have a signature match....
    }
}



回答2:


Usage (for a pdf file):

Assert.AreEqual("25504446", GetMagicNumbers(filePath, 4));

Method GetMagicNumbers:

private static string GetMagicNumbers(string filepath, int bytesCount)
{
    // https://en.wikipedia.org/wiki/List_of_file_signatures

    byte[] buffer;
    using (var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
    using (var reader = new BinaryReader(fs))
        buffer = reader.ReadBytes(bytesCount);

    var hex = BitConverter.ToString(buffer);
    return hex.Replace("-", String.Empty).ToLower();
}



回答3:


You can't. StreamReader is made to read text, not binary. Use the Stream directly to read bytes. In your case FileStream.

To guess whether a file is text or binary you could read the first 4K into a byte[] and interpret that.

Btw, you tried to force chars into bytes. This is invalid by principle. I suggest you familiarize yourself with what an Encoding is: it is the only way to convert between chars and bytes in a semantically correct way.



来源:https://stackoverflow.com/questions/14797664/original-file-bytes-from-streamreader-magic-number-detection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!