zomgistania

Main page | About | Articles | Previous Posts | Archives

Wednesday, April 26, 2006

Thinking about reinventing the wheel (withoug knowing about it)

A meaningful (?) post for a chance.


So, I am working on a bunch of classes which could handle some simple archives... file bit like .zips for example. So I think how should I implement this?

1. Maybe use XML... would work.
2. Maybe use file streams and make your own archive format (I've done this before with C++)
3. Use the one I made with C++

I scrapped the last idea since I'm quite sure that the library was quite bad :)


So I thought about making it with XML... That would be simple with the .NET built-in XML classes. The parsing of files from the package would be simple too.

but guess what?

The good folks at Microsoft had already done this! The classes at System.Resources starting with ResX.

Just give the ResXResourceWriter class a filename and call AddResource to add your file data into the file.
It takes a byte array, which can be easily obtained with System.IO.File.ReadAllBytes. Just pass your soon-to-be-packaged file as a parameter.

Extracting files from a ResX file is very simple too. Just use ResXResourceReader (or ResXResourceSet) and use it's GetEnumerator() with a IDictionaryEnumerator. The key will contain the resource's name and value shouod contain the byte array containing the data.

MSDN article on iterators, recommended reading about implementing IEnumerable into your own classes.
MSDN - System.Resources Namespace

Note that there's also another way to save resources in that namespace... Resource* classes, they save the stuff into a more obscure format if you like that more. I have no idea which one is better in terms of performance though.

The reader and writer both can do IO.Streams too. That means you, for example, do something like decompress the package file before extracting the resources and compress it again after saving.

So basically both my options one and two would've just been "reinventing the wheel". Oh well, I actually like doing that ;)

0 Comments:

Post a Comment

<< Home