.pak file reading example (libzzip)

Started by
-1 comments, last by umbrae 18 years, 8 months ago
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;
}

This topic is closed to new replies.

Advertisement