Skip to content

WDL ~ Looking into options to store JSON objects

adinilfeld edited this page May 13, 2021 · 3 revisions

This document is aimed at determining the feasibility of using Libzip to compress several JSON in the directory structure we are planning to have for WDL++. Currently, we can only support using a single file as the parser in libobj has a path to a file for the directory as the parser assumes the existence of only one file. Clearly, this will need an overhaul for a better system to store JSON objects if we are planning to include several features such as sounds and graphics that will require images to go with the JSON files and the information stored will have to be spread across several JSON files for the RPG teams. Using libzip is a possible solution to the storage of multiple files.

What does libzip do?

https://libzip.org/documentation/libzip.html

Summary: Libzip has open and close commands used to open and close zip archives. When Libzip is open, you are able to add files and directories. To use libzip, you would need to include #include <zip.h>

Explanation of useful commands: In order to get zip_t *archive that will be parse: zip_open(const char *path, int flags, int *errorp);

This function allows us to open the zip archive to use the functions for opening, reading and closing files.

zip_close(zip_t *archive); zip_discard(zip_t *archive);

Both of these functions close the archive, where zip_close saves changes made to the zip archive. However, there is no reason for us to be changing the zip archive as we are parsing the contents from the zip files so there is no reason for us to use zip_close instead of zip_discard.

zip_fopen(zip_t *archive, const char *fname, zip_flags_t flags);

This opens file given the archive and filename where libzip looks up the file. This will allow us have the file open with zip_file_t* type. The flags determine whether the data within the opened file is the original data or the compressed version. We will use whichever flag allows us to allow zip_fread work properly, which is the original data.

zip_fread(zip_file_t *file, void *buf, zip_uint64_t nbytes);

This function allows us to read a file using libzip, where the 2nd arguement is a buffer where the bytes read from the given file (the file from zip_fopen) is moved to. Json-c will then parse the resulting buffer from this function.

zip_fclose(zip_file_t *file);

This function closes the file that will be opened by zip_fopen.

Why use zip rather than leaving the files unzipped?

Source: https://www.winzip.com/win/en/aboutzip.html Using zip will compress the files to make certain actions faster. One of these actions is storing data, where storing compressed files will be more efficient in the long run compared to not zipping up the files. Speed will matter when more components to the game get added through various RPG teams and sound/image teams as that will require more data to be saved/loaded. However, this will require the user to have a zipped file with their contents instead of an unzipped file but zipping a file is an action that most people know how to do.

The compressed files also save space, therefore making it possible to save a larger amount of components of the game.

Is libzip the best zip file editor in C?

I looked into possible alternatives to libzip. One of them was kuba--/zip. Source: https://github.com/kuba--/zip This tool seems to have some of the commands in libzip just under different names. An advantage this alternative provides is sample uses of the library to make implementation easier. However, the functions that this alternative provides is limited. This alternative emphasizes being simple, but this will give up some of the functionality that libzip provides such as providing functions to unchange recent changes, functions that handle zip errors, and more. So based on my findings, libzip is the best file editor that has a decent usage.

Clone this wiki locally