Search code examples
htmlgzipzlibdeflatehttp-compression

How can I compress a char array into a compressed html page using Zlib


I have a CGI application in C that creates an html page by saving a char* as a html page:

void saveTextFile(const char *filename, const char *str){.......}  

called as

saveTextFile("..\\index.html",outputFile);

How do I use zlib to take as input the "outputFile" char array and output a zipped html page with appropriate headers?

Would the gzopen be used here instead of my saveTextFile function?

Any advice is appreciated. Thanks.


Solution

  • Got it -

    //****************************************************************************************
        //ZIP file: Take the char string "outputFile" as input to gzip. Create a zipped html file
        file = gzopen("..\\index.html.gz", "wb"); //create a zipped file
        if (file == NULL) {
            printf("Can't open zipped file");
            exit(1);
        }
        len   = strlen(outputFile); //need to know length of string
        compd = gzwrite(file, outputFile, (unsigned)len); //compress data into .gz file
        cls   = gzclose (file); //close .gz file
        //End zip***********************************************************************************