Skip to content

Instantly share code, notes, and snippets.

@jayaregalinada
Created August 22, 2018 10:12
Show Gist options
  • Save jayaregalinada/19b9c38e0eec1e0a11c504fe85474286 to your computer and use it in GitHub Desktop.
Save jayaregalinada/19b9c38e0eec1e0a11c504fe85474286 to your computer and use it in GitHub Desktop.
File content compress using gzcompress and gzuncompress
<?php
// Make sure you have a file.txt within this directory.
/** @var string File path location */
$fileLocation = __DIR__ . DIRECTORY_SEPARATOR . "file.txt";
/** @var Streams The stream of the file. */
$fileStream = fopen($fileLocation, 'r');
/** @var Streams|string The content of the file. */
$read = fread($fileStream, filesize($fileLocation));
// http://php.net/manual/en/function.gzcompress.php
$compress = gzcompress($read);
// Print the compress content.
echo join([
'<pre>',
$compress,
'</pre><pre>',
'String length ',
strlen($compress),
'</pre>'
]);
// http://php.net/manual/en/function.gzuncompress.php
$uncompress = gzuncompress($compress);
echo '<hr />';
// Print the uncompress content.
echo join([
'<pre>',
$uncompress,
'</pre><pre>',
'String length ',
strlen($uncompress),
'</pre>'
]);
die();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment