gzutils

The functions in the module gzutils allow the reading and writing of gzipped files.

Code:

Functions

createInstance

static gzUtils *createInstance(uint iBlockSize);

Creates a gzUtils instance with the specified block size.

iBlockSize

Size of blocks to use.

Returns a gzUtils instance on success, or NULL on failure.

do_gzip

int do_gzip(const std::string sInput, const std::string sOutput);

The contents of the input file are gzipped and written to the output file.

sInput

Name of the input file.

sOutput

Name of the output file (will be a gzipped file).

Returns 0 on success, -1 on failure (read or write error)

do_gunzip

int do_gunzip(const std::string sInput, const std::string sOutput);

The contents of the input file are gunzipped and written to the output file.

sInput

Name of the input file (should be a gzipped file)

sOutput

Name of the output file.

Returns 0 on success, -1 on failure (read or write error)

constructor

gzUtils();

The constructor.

init

int init(uint iBlockSize);

Allocates a buffer with specified size.

destructor

~gzUtils();

The destructor. Frees the memory used for the buffer.

Example

Compile with

g++ -g  gz_example.cpp  gzutils.cpp -lz -o gz_example

Code for gz_example:

#include <cstdio>

#include "gzutils.h"


int main(int iArgC, char *apArgV[]) {
    int iResult = 0;
    bool bgzip = true;
    const int GZ_BLOCK_SIZE = 8192;

    if (iArgC > 3) {
        std::string sMode(apArgV[1]);
        std::string sInput(apArgV[2]);
        std::string sOutput(apArgV[3]);

        gzUtils *pgz = gzUtils::createInstance(GZ_BLOCK_SIZE);
        if (pgz != NULL) {
            if (sMode == "-g") {
                iResult = pgz->do_gzip(sInput, sOutput);
            } else if (sMode == "-u") {
                iResult = pgz->do_gunzip(sInput, sOutput);
            } else {
                iResult = -1;
            }

            delete pgz;
        } else {
            printf("Couldn't gzUtils instance\n");
            iResult = -1;
        }
    } else {
        printf("usage: %s [\"-g\" | \"-u\"] <input-file> <output-file>\n", apArgV[0]);
        iResult = -1;
    }


    return iResult;
}