简体   繁体   中英

How to write a file format handler

Today i'm cutting video at work (yea me!), and I came across a strange video format, an MOD file format with an companion MOI file.

I found this article online from the wiki, and I wanted to write a file format handler, but I'm not sure how to begin.

I want to write a file format handler to read the information files, has anyone ever done this and how would I begin?

Edit:

Thanks for all the suggestions, I'm going to attempt this tonight, and I'll let you know. The MOI files are not very large, maybe 5KB in size at most (I don't have them in front of me).

You're in luck in that the MOI format at least spells out the file definition. All you need to do is read in the file and interpret the results based on the file definition.

Following the definition, you should be able to create a class that could read and interpret a file which returns all of the file format definitions as properties in their respective types.

Reading the file requires opening the file and generally reading it on a byte-by-byte progression, such as:

        using(FileStream fs = File.OpenRead(path-to-your-file)) {
            while(true) {
                int b = fs.ReadByte();
                if(b == -1) {
                    break;
                }
                //Interpret byte or bytes here....
            }
        }

Per the wiki article's referenced PDF , it looks like someone already reverse engineered the format. From the PDF, here's the first entry in the format:

Hex-Address: 0x00
Data Type: 2 Byte ASCII
Value (Hex): "V6"
Meaning: Version

So, a simplistic implementation could pull the first 2 bytes of data from the file stream and convert to ASCII, which would provide a property value for the Version.

Next entry in the format definition:

Hex-Address: 0x02
Data Type: 4 Byte Unsigned Integer
Value (Hex): 
Meaning: Total size of MOI-file

Interpreting the next 4 bytes and converting to an unsigned int would provide a property value for the MOI file size.

Hope this helps.

If the files are very large and just need to be streamed in, I would create a new reader object that uses an unmanagedmemorystream to read the information in.

I've done a lot of different file format processing like this. More recently, I've taken to making a lot of my readers more functional where reading tends to use 'yield return' to return read only objects from the file.

However, it all depends on what you want to do. If you are trying to create a general purpose format for use in other applications or create an API, you probably want to conform to an existing standard. If however you just want to get data into your own application, you are free to do it however you want. You could use a binaryreader on the stream and construct the information you need within your app, or get the reader to return objects representing the contents of the file.

The one thing I would recommend. Make sure it implements IDisposable and you wrap it in a using!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM