Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About PHP and gz-compression

I was looking for gz-compression in PHP and I found this piece of code:

<?php
function print_gzipped_output()
{
    $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
    if( headers_sent() )
        $encoding = false;
    else if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
        $encoding = 'x-gzip';
    else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false )
        $encoding = 'gzip';
    else
        $encoding = false;

   if( $encoding ) {
        $contents = ob_get_clean();
        $_temp1 = strlen($contents);
        if ($_temp1 < 2048)    // no need to waste resources in compressing very little data
            print($contents);
        else
        {
            header('Content-Encoding: '.$encoding);
            print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
            $contents = gzcompress($contents, 9);
            $contents = substr($contents, 0, $_temp1);
            print($contents);
        }
    }
    else
        ob_end_flush();
}
?>

My question is simple: what does the line

print("\x1f\x8b\x08\x00\x00\x00\x00\x00");

actually means?

Thank you in advance

like image 720
mneri Avatar asked Mar 17 '26 14:03

mneri


1 Answers

This is the header for gzip-format files. You can view more details here.

The first two bytes identify the file as gzipped. The following 8 specifies the use of the DEFLATE compression method. The final four zero bytes are for fields which aren't needed.

like image 146
Michael Mior Avatar answered Mar 19 '26 02:03

Michael Mior