Skip to content

Instantly share code, notes, and snippets.

@stephanschuler
Created January 15, 2020 13:39
Show Gist options
  • Save stephanschuler/212bdf920c3327f78e1024a1bf6c0350 to your computer and use it in GitHub Desktop.
Save stephanschuler/212bdf920c3327f78e1024a1bf6c0350 to your computer and use it in GitHub Desktop.
mysql_compress and mysql_uncompress
<?php
declare(strict_types=1);
function mysql_compress($base64 = null): string
{
if (!$base64) {
return '';
}
$size = pack('V', strlen((string)$base64));
$body = gzcompress($base64, 9, ZLIB_ENCODING_DEFLATE);
return base64_encode($size . $body);
}
$content = file_get_contents('php://stdin');
echo mysql_compress($content);
<?php
declare(strict_types=1);
function mysql_uncompress($base64 = null): string
{
if (!$base64) {
return '';
}
$binary = base64_decode((string)$base64);
$size = current(unpack('V', substr($binary, 0, 4)));
$body = substr($binary, 4);
// FIXME: Should be `gzuncompress($body, $size)` but some data is invalid.
$result = gzuncompress($body);
return $result;
}
$content = file_get_contents('php://stdin');
echo mysql_uncompress($content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment