PHP 8.1.28 Released!

gzcompress

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

gzcompressСжать строку

Описание

gzcompress(string $data, int $level = -1, int $encoding = ZLIB_ENCODING_DEFLATE): string|false

Данная функция сжимает строку используя формат данных ZLIB.

Подробнее про алгоритм сжатия ZLIB смотрите документ "» Формат сжатия ZLIB. Спецификация версии 3.3" (RFC 1950).

Замечание:

Это не то же самое, что и gzip-сжатие, которое включает в себя некоторые данные заголовка. Информацию о gzip-сжатии смотрите gzencode().

Список параметров

data

Данные для сжатия.

level

Уровень сжатия. Целое число от 0 до 9 (0 - без сжатия, 9 - максимальное сжатие).

Если используется значение -1, то будет установлен принятый в библиотеке zlib по умолчанию уровень сжатия, который равен 6.

encoding

Одна из констант ZLIB_ENCODING_*.

Возвращаемые значения

Сжатая строка или false в случае возникновения ошибки.

Примеры

Пример #1 Пример использования gzcompress()

<?php
$compressed
= gzcompress('Сожми меня', 9);
echo
$compressed;
?>

Смотрите также

  • gzdeflate() - Сжимает строку
  • gzinflate() - Распаковать сжатую строку
  • gzuncompress() - Распаковать сжатую строку
  • gzencode() - Создать сжатую строку gzip

add a note

User Contributed Notes 2 notes

up
6
@boas.anthro.mnsu.edu
23 years ago
No, it doesn't return gzip compressed data -- specifically, the CRC is messed up. However, after massaging the output a lot, I have come up with a solution. I also commented it a lot, pointing out odd things.

<?php
// Start the output buffer
ob_start();
ob_implicit_flush(0);

// Output stuff here...

// Get the contents of the output buffer
$contents = ob_get_contents();
ob_end_clean();

// Tell the browser that they are going to get gzip data
// Of course, you already checked if they support gzip or x-gzip
// and if they support x-gzip, you'd change the header to say
// x-gzip instead, right?
header("Content-Encoding: gzip");

// Display the header of the gzip file
// Thanks ck@medienkombinat.de!
// Only display this once
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";

// Figure out the size and CRC of the original for later
$Size = strlen($contents);
$Crc = crc32($contents);

// Compress the data
$contents = gzcompress($contents, 9);

// We can't just output it here, since the CRC is messed up.
// If I try to "echo $contents" at this point, the compressed
// data is sent, but not completely. There are four bytes at
// the end that are a CRC. Three are sent. The last one is
// left in limbo. Also, if we "echo $contents", then the next
// byte we echo will not be sent to the client. I am not sure
// if this is a bug in 4.0.2 or not, but the best way to avoid
// this is to put the correct CRC at the end of the compressed
// data. (The one generated by gzcompress looks WAY wrong.)
// This will stop Opera from crashing, gunzip will work, and
// other browsers won't keep loading indefinately.
//
// Strip off the old CRC (it's there, but it won't be displayed
// all the way -- very odd)
$contents = substr($contents, 0, strlen($contents) - 4);

// Show only the compressed data
echo $contents;

// Output the CRC, then the size of the original
gzip_PrintFourChars($Crc);
gzip_PrintFourChars($Size);


// Done. You can append further data by gzcompressing
// another string and reworking the CRC and Size stuff for
// it too. Repeat until done.


function gzip_PrintFourChars($Val)
{
for (
$i = 0; $i < 4; $i ++)
{
echo
chr($Val % 256);
$Val = floor($Val / 256);
}
}
?>
up
3
detain at interserver dot net
6 years ago
gzipped strings include header/metadata you can use to determine if a string is gzipped or not , but since gzcompress does not include that I found myself needing a way to determine if a string was compressed or not. After some research (and then improvements) i came up with this:

/**
* determines if a string is a gzipped string supporting strings
* encoded with either gzencode or gzcompress
*
* @param string $string the string to check for compression
* @return bool whether or not the string was compmressed
*/
function is_gzipped($string) {
return mb_strpos($string, "\x1f\x8b\x08", 'US-ASCII') === 0 && @gzuncompress($string) !== FALSE;
}
To Top