How to modify and zip back a gresource file in Ubuntu (18.04/20.04)

List, extract and compress/zip gresource file

What will we learn ?

What does gresource files mean. Walk through on how to view the contents of a gresource file, modify it and zip it back with a gresource extension. Please note the file path may vary for different versions. But the process can be used for any gresource files. Let’s begin !!

What is gresource ?

Applications and libraries often contain binary or textual data that is really part of the application, rather than user data. For instance GtkBuilder .ui files, splashscreen images, GMenu markup XML, CSS files, icons, etc. These are often shipped as files in $datadir/appname, or manually included as literal strings in the code.

The GResource API and the glib-compile-resources program provide a convenient and efficient alternative to this which has some nice properties. You maintain the files as normal files, so its easy to edit them, but during the build the files are combined into a binary bundle that is linked into the executable. This means that loading the resource files are efficient (as they are already in memory, shared with other instances) and simple (no need to check for things like I/O errors or locate the files in the filesystem). It also makes it easier to create relocatable applications.

List and extract Gresource file contents

To view the contents of a gresource file you can use the following command.

gresource list fileName.gresource

Example: gresource list gnome-shell-theme.gresource

Okay, so now we saw the list of files present. Next we will see how to extract these files. There are two ways to do this.

Method 1: Use the extract command from gresource as follows. The gresource extract command extracts and write the extracted contents onto stdout and does not directly write to a file on disk, so redirection to a file is needed.

gresource extract fileName.gresource filePathFromList > newFileName

Example: gresource extract gnome-shell-theme.gresource /org/gnome/shell/theme/calendar-arrow-left.svg > calendar-arrow-left.svg

Gresource list and extract commands

Method 2: Write a shell script :) This save much of your time if there are too many files that are to be extracted. The script creates a shell-theme/theme folder where all the files will be extracted.

#!/bin/sh

workdir=${HOME}/shell-theme
if [ ! -d ${workdir}/theme ]; then
mkdir -p ${workdir}/theme
fi
gst=/usr/share/gnome-shell/gnome-shell-theme.gresource

for r in `gresource list $gst`; do
gresource extract $gst $r >$workdir/${r#\/org\/gnome\/shell/}
done

Zip back the gresource file

Now!! The most awaited instance to zip back the file content. Based on the method followed for extracting choose appropriately.

Before zipping the gresource file make sure you have extracted all the images (png/svg etc), css files in one folder although you might have not edited it. When the gresource file is zipped the number of files should remain same.

Method 1: Create a gnome-shell-theme.xml file where all the images, css files are extracted and use the following XML (eXtensible Markup Language) code.

Method 2: Traverse to shell-theme/theme folder created by the shell script as mentioned above and create a file gnome-shell-theme.xml and use the following XML (eXtensible Markup Language) code.

<?xml version =”1.0” encoding=”UTF-8”?>

<gresource>

<gresource prefix=”/org/gnome/shell/theme”>

<file>calendar-arrow-left.svg</file>

<file>gnome-shell.css</file>

….

<file>ws-switch-arrow-up.png</file>

</gresource>

</gresource>

Note: A gresource folder can also consist of different prefixes. Just go ahead and add another gresource tag with appropriate prefix. Do not forget to segregate all folder paths in different gresource tags. You can always find the exact folder paths in the gresource list (gresource list folderName.gresource).

To generate the gresource file run the below command which will zip/compress back all the contents (img, css etc) in a single gresource file

glib-compile-resources fileName.xml

Example: glib-compile-resources gnome-shell-theme.xml

The gnome-shell-theme.gresource file will be created with the following contents:

/org/gnome/shell/theme/calendar-arrow-left.svg

/org/gnome/shell/theme/gnome-shell.css

/org/gnome/shell/theme/ws-switch-arrow-up.png

Note: Check for the file permissions. You can easily modify the permission using chmod command. 755 gives rwx-r-x-r-x permissions for file, group and others.

Example: chmod +755 gnome-shell-theme.gresource