About

Gzip utils is a set of utility scripts to work with gzip, tar and, to a lesser extent, zip files.

You can use Gzip utils to extract the contents of .gz and .tar files, and read the metadata of .gz, .tar and .zip files, listing for example the files contained in an archive with their attributes without actually expanding it.

Moreover, this a GML only asset, you don't need any extension to use it. This means out of the compatibility with almost every export module that supports buffer operations (with the exception of HTML5 and Javascript based exports).

Limits

  • This is not a compression utility. All the provided functionalities deal with reading and extracting archives.
  • Due to HTML5 being able to load only Base64 encoded buffers, this asset does not work properly on HTML5 and javascript based export modules.
  • Files can be extracted only inside the sandbox. On windows, you can load files outside of the sandbox with get_open_filename, but the files contained in the archives can only be extracted inside of it.
  • Install

    Open GameMaker: Studio, log in to the Marketplace and navigate to the Library tab. From here you can load the extension resource into your project.
    If you want to test it out, import all resources into an empty project. Otherwise just import the scripts.

    Function reference

    GZIP

    gzip_close(file_handle)

    Closes an open gzip file opened using gzip_open(). This frees the memory from the file data.

    var gzip = gzip_open("sample.gz");
    if(gzip >= 0) { /* ...read file data... */ gzip_close(gzip); }
    else { show_message("Error loading gzip file!"); }
    gzip_open(fname)

    Opens a gzip file, and returns its handle (or a negative number if there is a load error).

    The returned file handle is a ds_map holding the gzip metadata and a buffer with the binary data. See the table at the bottom of this section for a list of available fields.

    The file has to be closed with gzip_close(handle) in order to free the memory.

    //get the name of the compressed file
    var gzip = gzip_open("sample.gz");
    if(gzip >= 0) { filename = gzip[? "fname"]; gzip_close(gzip); }
    else { show_message("Error loading gzip file!"); }
    gzip_unzip(fname,dest,[untar])

    Decompresses and extracts a gzip file at a specified location inside the sandbox, and returns the number of extracted files (or a negative number in case of errors). You don't need to use gzip_open() or gzip_close() in order to use this function, and in fact, I advise against it.

    dest is the path where the files will be extracted, relative to the sandbox directory, without the starting backslash. An empty string will therefore extract the files in the root of working_directory.

    untar (optional, default = true) if false does not try to untar the decompressed file. If true or omitted, if the gzip holds a tar archive, it is automatically expanded.

    //comment var extracted_files = gzip_unzip("sample.tar.gz","my_folder/"); if(extracted_files >= 0) {show_message(string(extracted_files) + " files extracted");} else {show_message("Error extracting files");}

    Gzip properties

    See RFC for details

    KeyTypeRequiredDescription
    compressed_size real Yes Size of the compressed data (in bytes)
    compression_method real Yes Compression method used (8 is the de facto standard and the only supported one)
    crc real Yes CRC checksum
    data buffer Yes Buffer holding the gzip data (with headers)
    data_offset real Yes Offest of the compressed data (from buffer start)
    fcomment string No File comment
    fextra boolean No Extra fields are present
    fhcrc fhcrc No CRC of the header file
    flags real Yes Flags
    fname string No Name of the compressed file
    ftext boolean Yes File contains ASCII text
    mtime datetime Yes Last file modification datetime
    os real Yes Operating system used for compressing
    uncompressed_size real Yes Size of the uncompressed data (in bytes)
    xflags real Yes Extra flags (compression type)

    TAR

    tar_close(file_handle)

    Closes an open tar file opened using tar_open(). This frees the memory from the file data.

    var tar = gzip_open("sample.tar");
    if(tar >= 0) { /* ...read file data... */ tar_close(tar); }
    else { show_message("Error loading tar file!"); }
    tar_open(fname)

    Opens a tar file, and returns its handle (or a negative number if there is a load error).

    The returned file handle is a ds_map holding the tar metadata and a buffer with the binary data. See the table at the bottom of this section for a list of available fields.

    The file has to be closed with tar_close(handle) in order to free the memory.

    //get a ds_list holding the files inside the archive (without extracting it)
    var tar = tar_open("sample.tar");
    if(tar >= 0) { files = ds_list_create(); ds_list_copy(files,tar[? "entries"]); tar_close(tar); }
    else { show_message("Error loading tar file!"); }
    tar_untar(fname,dest)

    Extracts the contents of a tar file at a specified location inside the sandbox, and returns the number of extracted files (or a negative number in case of errors). You don't need to use tar_open() or tar_close() in order to use this function, and in fact, I advise against it.

    dest is the path relative to the sandbox directory where the files will be extracted, without the starting backslash. An empty string will therefore extract the files in the root of working_directory.

    //comment var extracted_files = tar_untar("sample.tar","my_folder/"); if(extracted_files >= 0) {show_message(string(extracted_files) + " files extracted");} else {show_message("Error extracting files");}

    Tar properties

    See RFC for details

    KeyTypeRequiredDescription
    data buffer Yes Buffer holding the tar data
    entries ds_list Yes List of the files in the archive, with their properties. Every entry is a ds_map structured as follows

    Entries properties

    This table lists the properties of a single entry of a tar file.

    KeyTypeRequiredDescription
    checksum string Yes File checksum
    data_offset real Yes Offset of the file data from buffer start
    devmajor string No Device major number
    devminor string No Device minor number
    filename string Yes Name of the file
    gid string Yes Group ID
    gname string No Owner group name
    linkname string Yes Symbolic link name
    mode string Yes File mode
    mtime datetime Yes Last modification datetime
    name_prefix string No Filename prefix (already computed in filename"
    size real Yes File size (in bytes)
    type string Yes Type of file
    uid string Yes User UD
    uname string No Owner user name
    ustar string No Ustar format fields a present
    version string No Ustar version

    ZIP

    zip_close(file_handle)

    Closes an open zip file opened using zip_open(). This frees the memory from the file data.

    var zip = zip_open("sample.zip");
    if(zip >= 0) { /* ...read file data... */ zip_close(zip); }
    else { show_message("Error loading zip file!"); }
    zip_open(fname)

    Opens a zip file, and returns its handle (or a negative number if there is a load error).

    The returned file handle is a ds_map holding the zip metadata and a buffer with the binary data. See the table at the bottom of this section for a list of available fields.

    The file has to be closed with zip_close(handle) in order to free the memory.

    //get a ds_list holding the files inside the zip archive (without extracting it)
    var zip = zip_open("sample.zip");
    if(zip >= 0) { files = ds_list_create(); ds_list_copy(files,zip[? "entries"]); zip_close(zip); }
    else { show_message("Error loading zip file!"); }

    Zip properties

    See RFC for details

    KeyTypeRequiredDescription
    data buffer Yes Buffer holding the zip data
    eocd_offset real Yes Offset of the end of central directory from buffer start
    cdfh_offset real Yes Offset of the first central directory file header from buffer start
    total_entries real Yes Number of files
    entries ds_list Yes List of the files in the archive, with their properties. Every entry is a ds_map structured as follows

    Entries properties

    This table lists the properties of a single entry of a tar file.

    KeyTypeRequiredDescription
    cdfh_offset real Yes Offset of this file central directory header, from buffer start
    comment string No Comment
    compressed_size real Yes Size of compressed file (in bytes)
    compression_method real Yes Compression method used
    crc real Yes Checksum
    datetime_modified datetime Yes Last modification time
    ext_attributes real Yes External file attributes
    filename string Yes Name of the file
    header_offset real Yes Offset of the local file header from buffer start
    os real Yes Operating system used for compression and file attributes
    uncompressed_size real Yes Size of uncompressed file (in bytes)
    version real Yes Zip version used for compression
    version_required real Yes Zip version required to decompress

    Other

    These scripts are used by the other gzip utils functions, but are generic enough to be useful for other purposes.

    date_create_dos(date,time)

    Creates a Game Maker datetime from a dos formatted date and time (as real numbers)

    date_create_unix(datetime)

    Creates a Game Maker datetime from a unix timestamp.

    date_dos_date(datetime)

    Returns a dos formatted date from a Game Maker datetime value.

    date_dos_time(datetime)

    Returns a dos formatted time from a Game Maker datetime value.

    filename_name_noext(filename)

    Returns the name of the specified file with the extension and path removed.

    //returns "assets"
    filename_name_noext("/data/assets.gz");

    Credits

    Contact me on the GMC forums or by email at simoneguerra<at>ekalia.com