4
Is there any way to shorten the size of the type string zipar
she and then deszipar
as the Base64 function does?
4
Is there any way to shorten the size of the type string zipar
she and then deszipar
as the Base64 function does?
4
You have at least two alternatives to compress:
This function compresses the string using the ZLIB data format.
Decompress with gzuncompress()
.
This function compresses the string using the DEFLATE data format.
Decompress with gzinflate()
.
A simple test reveals that gzdeflate
achieves better results:
$tring = "Bubu votou para fechar!";
$compressed1 = gzcompress($tring, 9);
echo strlen($compressed1).PHP_EOL; // 31 bytes
$compressed2 = gzdeflate($tring, 9);
echo strlen($compressed2).PHP_EOL; // 25 bytes
See test on Ideone.
The improvement of results obtained in such a simple string is related to the fact that the function gzcompress()
adds a 2 byte header and a 4 byte verification value at the end:
In terms of compression, both present the same performance, but in terms of decompression, mainly with large data, the function gzinflate()
is faster, performed almost half the time when compared to the gzuncompress()
.
In short:
If the data to compress is staying on the same machine, gzdeflate()
seems to be the ideal option.
In terms of portability, the solution could pass through a third function.
To compress a string on a machine and decompress on a different machine, it is convenient to have some information about the work done:
This function returns a compressed version of input data compatible with gzip program output.
That is, the output of the function contains the headers and the data structure, giving us a way to "move" the string safely compressed.
Decompress with gzdecode()
.
A simple test reveals an increase in output size due to the control information present in it (headers and data structure):
$tring = "Bubu votou para fechar!";
$compressed3 = gzencode($tring, 9);
echo strlen($compressed3).PHP_EOL; // 43 bytes
See test on Ideone.
3
Maybe this is what you’re looking for:
Function gzcompress: used to compress a string, returning another string with the compressed content.
Function gzuncompress: used to decompress a compressed string. Returning the original string.
$compressed = gzcompress('Compress me', 9);
$decompress = gzuncompress($compressed);
echo $compressed."\n";
echo $decompress;
Output:
x�s��-(J-.V�M��?
Compress me
Note: the compressed output was larger than the original, because the algorithm adds additional information to the compressed string, so it can decompress later. The larger the original string, the better the result.
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
you have these functions
gzdeflate
for Javascript as well?– Vinícius Lara
@user3163662 Sorry for the delay, as Javascript is off-topic here in this PHP question, I have prepared a Question-and-Answer for such a solution in Javascript: http://answall.com/a/57391/223
– Zuul
+1 Gosh, I knew I had a blast of life.
– Miguel Angelo