I look at C++ like using a chainsaw with the guards taken off...it's powerful, but really easy to cut your arm off
I was thinking about some simple steps for you and here's what I came up with:
Before you begin, go to the HackersQuest forums and search for S3D. This is where I found all my information on S3D files. There is a post there that does a terrific job of documenting the format.
My steps for writing an S3D file:
------------------------------------------------------
1. Make an index list of your files, sorted by lowercase filename. I used a numbered list, which should work well for this purpose.
2. For each of your files, break it into 8k blocks and compress each block, but *don't* write them to the archive yet (for each file you should now have a list of compressed blocks).
3. Create another block that consists of the filenames in the archive, sorted by lowercase filename (see step 1). The first long in the block is the number of filenames, then for each filename there is a long containing its length (*including* null terminator) and the filename data itself.
4. Just as you did for the files, break up the filename data block into 8k chunks and compress the chunks (usually it's just one block, but do it right and assume it can be more).
5. Now it's time to make the directory entries. Stepping through your filename data in order of lowercase filename, create a directory entry for each file and assign its filename position, starting at sizeof(header) and counting up. The reason for all of this is because the order of the filenames in the filename data block *must* match the order in which the filename data is actually written to disk. In every .S3D file I've seen, they're written ordered by lowercase filename. The order of directory entries at the end of the archive doesn't have to be the same since they contain the actual file positions of the file data.
6. Now create one more directory entry for the filename data block. The filename data blocks *always* come after the other file data blocks. The directory entry has a fixed CRC value. You can find it in S3D.PAS.
7. Now it's time to write to disk:
7a. Write the header.
7b. Stepping through your files in order of lowercase filename, write the data blocks for each file. Remember that each block has to have a block header with it.
7c. Write the data blocks for the filename data. 99 times out of 100 it's just one block, but you should do it right and handle it just like any other file.
7d. Write a long that says how many directory entries there are.
7e. Write the directory entries, in order of increasing CRC value. Remember that there is a directory entry for the filename data block(s).
7f. Write the footer. Note that, if you're writing an .S3D that was previously read, that not all .S3D files have a footer. A reader shouldn't fail if it doesn't find it and in that case it shouldn't write one, since it doesn't know what to put in the date field (this is something I have to do in the next version of OpenZone). If you're creating one from scratch, I find that putting anything in there works just fine, but otherwise you need to preserve what was already there. The docs on Hackersquest describe it as a possible CRC, but I've convinced myself that is has absolutely nothing to do with the S3D name or its contents. I believe it's a pseudo-datestamp that the patcher uses to quickly determine a file's version.