• Advertisement
Sign in to follow this  

.pak file reading example (libzzip)

This topic is 4585 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

This isn't particularly related to game programming but is used mostly for games so I hope this is the right place to post it. I wanted to get the whole transparent .pak type file functionality working in my engine but I couldn't find anywhere with a solid example of how to do it with the libzzip library. I always find it annoying when a library doesn't have an example of how to use it (this also happens with man pages and unix commands)... but I'm ranting. So I spent some time working out how to do it, it's actually really easy but takes time reading the documentation. Anyway I came up with some example code which I hope will help other people and possibly let you know how easy it is to add this functionality to your engine. Setting out of the error handling could be done better, please post with any errors and/or modifications to the code.
#include <stdio.h>
#include <zzip/lib.h>

int main(int argc, char * const argv[])
{
    // we are just going to read the first 10 characters out of the file
    char* array;
    array = new char[11];
    
    // what file endings to accept... "" means that you can use /test.zip/
    // in the file name - has to end with a zero
    static zzip_strings_t ext[] = { ".zip", ".pak", "", 0 };
    
    // these functions are exactly the same as the normal posix-IO ones
    ZZIP_FILE * file;
    // any of the directories in the path can be zip files... but they can't
    // be nested. eg this opens the zipfile.zip (or .pak) file that has a directory
    // called test holding the file data.dat
    file = zzip_open_ext_io ("zipfile/test/data.dat", O_RDONLY, 0, ext, NULL);
    if (file == NULL)
    {
        printf("could not open file\n");
        return 1;
    }
    if (zzip_fread(array , sizeof(char) , 10 , file))
    {
        array[10] = '\0';
    
        printf("%s\n", array);
    }
    else
    {
        printf("could not read file\n");
    }
    zzip_close (file);
    
    delete[] array;
	
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement