Search code examples
c++archive

How to archive and compress data with "PhysicsFS"


I'm looking in "PhysicsFS" documentation and search for way to archive and compress data but can't find.Is it possible and if it is how i can do that


Solution

  • PhysicsFS zip support

    PhysicsFS has support for reading files from a zip file mounted at an arbitrary point in the "virtual filesystem" that it provide. This effectively provide decompression from a ZIP archive.

    However, PhysicsFS has no support to add or modify the content of a ZIP archive. It only permit to write uncompressed files in what is called "the write directory" in its documentation.

    So, to summarize: PhysicsFS only support reading from ZIP archives, not writing to it. For the compression you are on your own: just zip all the written files using an external compressor if you need so.


    PhysicsFS usage

    There is a small tutorial for PhysicsFS here.

    It is very simple to use:

    // initialize the lib
    PHYSFS_init(NULL);
    
    // "mount" a zip file in the root directory
    PHYSFS_AddToSearchPath("myzip.zip", 1);
    
    // set a directory for writing
    PHYSFS_setWriteDir(const char *newDir);
    
    // open a file for reading
    PHYSFS_file* myfile = PHYSFS_openRead("myfile.txt");
    
    // open a file for writing
    PHYSFS_file* myfile = PHYSFS_openWrite("output_file.bin");
    
    // get a file size
    PHYSFS_sint64 file_size = PHYSFS_fileLength(myfile);
    
    // read data from a file (decompress only if path is inside a zip mount point)
    char* myBuf = new char[file_size];
    int length_readed = PHYSFS_read(myfile, myBuf, 1, file_size);
    
    // write data to a file (uncompressed)
    char* myBuf = new char[new_file_size];
    //...fill myBuf...
    int length_writed = PHYSFS_write(myfile, myBuf, 1, new_file_size);
    
    // close a file
    PHYSFS_close(myfile);
    
    // deinitialize the lib
    PHYSFS_deinit();