Sign in to follow this  

Reading .PAK files

This topic is 4056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone. I'm a little confused about how this code is working. This is from cmdlib.c in the HL 1 SDK. Now I'm trying to learn what this code is doing because I need to learn to manipulate the way the program reads and understands the .PAK files. I don't understand anything about this code and was wondering if you guys could help. I don't think this is cpp either, I'm pretty sure this is c and I don't know anything about c. Here's the Code: Taken from HL SDK 2.0: [Quote]/* ============ ListPak Prints the contents of the specified pak file to stdout ============ */ void ListPak(char* pakname) { FILE* f = SafeOpenRead(pakname); packheader_t head; packfile_t* pdir; long i=0,imax=0; long totlen=0; SafeRead(f,&head,sizeof(packheader_t)); pdir=malloc(head.dirlen); fseek(f,head.dirofs,SEEK_SET); SafeRead(f,pdir,head.dirlen); fseek(f,0,SEEK_END); totlen=ftell(f); fclose(f); imax=head.dirlen/sizeof(packfile_t); for(i;i<imax;i++) { printf ("%64s : %7i\n", pdir[i].name,pdir[i].filelen); } printf("Total Length: %li bytes\n",totlen); } long flen(FILE* f) { long savepos=ftell(f); long filelen=0; fseek(f,0,SEEK_END); filelen=ftell(f); fseek(f,savepos,SEEK_SET); return filelen; }[/Quote] I just need help understand what's going on here, what is it doing and how can I convert it to cpp. Thanks, Taylor (Odin)

Share this post


Link to post
Share on other sites
Hey, What do you mean by "how can I convert it to cpp" ?

Well the "void ListPak(char* pakname)" function reads the PAK file and store the information from it in 2 structures called:

packheader_t head;
packfile_t* pdir;

then it prints out the names of all the files stored in it and the size of them.

the long flen(FILE* f) function just return the size of the file.

Anyway:

Try to call this in your program.

ListPak( "MyPakFile.pak" );

Good luck.

Share this post


Link to post
Share on other sites



You need to understand the Format of a Pak file.


There is usually a static header (fixed struct) and then a directory list of all the subentities (directory entries for the files).

That is then followed by all the file images, linearly.


The program first reads in the header and then the entire directory (array of file names/data structs) -- it knows how big the dir block is from data in the header.

It then prints the individual file names (traversing the array with a for loop)


There is a .h file that defines the Structs involved.

Share this post


Link to post
Share on other sites

This topic is 4056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this