PDA

View Full Version : resource file support?



Paulius
27-11-2002, 05:57 AM
A game i'm working on uses a lot of small files and the waste of disk space is huge (2MB ocupy around 4MB on disk). I was wondering if there was some free component to support zip files. I don't really care about compresion, i just want to have one resource file with directory structure in it.

Alimonster
27-11-2002, 09:30 AM
Have a look through Torry (http://www.torry.net) and The Delphi Super Page (http://delphi.icm.edu.pl/). A quick browse found heaps of zip components, many of which are freeware.

Alternatively, simply pack the file yourself using lots of blockreads/blockwrites. You'd have a helper application that stored all the files you packed together in a table. You would do this:

1) create a new file
2) empty out the table
3) for each file in dir...
3.1) add file name, size to table
3.2) blockwrite the file to the packed file
4) write out the header once done to the end
5) write out the size of the header

Then, when reading in the file, you would see to the end of the file minus however-many-bits for the size of the header (e.g. four 4 bytes for an integer size). You'd then allocate that amount of space for a new header and would seek back to the header start in the file.

Once you have the header, you can get the name/offset of any file in there.

Uh, the above is from the top of my head and may not work correctly. I'll do a working demo of it when I get back home.

There's also a tutorial over at http://epicboy.flipcode.com/ describing how to use zip files to combine stuff but with no compression - what you want, but it's in C++.

Paulius
27-11-2002, 02:18 PM
Thanks for your reply Alimonster, but all free components i find simply extract files to disc. Can anyone give me the name of a component which whould extract files in the memory. Or i'll just wait for Alimonster's demo.

{MSX}
27-11-2002, 04:38 PM
Can anyone give me the name of a component which whould extract files in the memory. Or i'll just wait for Alimonster's demo.
This is simple to obtain.. just do what alimonster said: create a small utility that write all the file you need on a single file (with the appropriate headers). Than you can load single chunks of the file in memory, for example using the TMemoryStream (great class!). With this you can load all your objects with the LoadFromStream method that is implemented in lots of classes

Alimonster
27-11-2002, 08:47 PM
I've hacked up an example program. Caveat emptor and all that stuff...

Get it here (http://www.alistairkeys.co.uk/download/packer.zip) (6K).

Start it up and you should hopefully see the contents of the packed file. You can select one in the listbox and hit the "View entry" button to see that bitmap. Of course, you can pack any amount of any type of file, not just bitmaps, but I did it for .bmps simply for clarity.

Don't push the interface too hard - I didn't design for stability, so it may explode. It's only for demo purposes here ;).

I hardly ever use the stream classes now, mind you. Anyway, you might want to structure the code a bit better (make it into a class, for example). However, the main code is there and it works.

Paulius
28-11-2002, 11:08 AM
Thanks a lot Alimonster, the domo was very helpful. I get one error with your demo on my system, maybe it's because i'm using Delphi 5?

procedure TfrmPackerMain.btnAddClick(Sender: TObject);
begin
if dlgOpen.Execute then
lstFiles.Items.Assign(dlgOpen.Files);

btnSave.Enabled:= (lstFiles.Count > 0);// i get undeclared identifier: 'Count'
end;

This isn't realy a problem, i just wanted to let you know.

Paulius
28-11-2002, 11:50 AM
And i think it should have been

lstFiles.Items.AddStrings(dlgOpen.Files);

nstead of

lstFiles.Items.Assign(dlgOpen.Files);

in TfrmPackerMain.btnAddClick

Alimonster
28-11-2002, 12:36 PM
Yeah, you're probably right on that last one w/ Assign vs AddStrings, btw :). I didn't spend too long on the interface, which is why I gave a disclaimer or two during my last post ;). There are probably more bugs around there too! I may write an article on this for my site w/ proper o.o. (tested!) code - until then, I'm afraid, you'll have to treat the interface as "possibly buggy." I'm not going to fix any bugs in there ^_^.

The other one about "lstFiles.Count" is obviously a new thing in Delphi 6 - it's a shortcut that simply maps to "lstFiles.Items.Count". I didn't realise that it wasn't in earlier Delphi versions (good to know for compatbility...). Ah well, you live, you learn. (The demo was made in Delphi 6.)

If you plan on turning it into a class, remember to make the variables "FFileSize" and "LastPack" into class members.