Skip to content

Commit

Permalink
Add support for zlib/level context option
Browse files Browse the repository at this point in the history
  • Loading branch information
sgolemon committed Oct 24, 2017
1 parent af1e6a8 commit 8f804a2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,7 @@ PHP NEWS
. Fixed unzserialize(), to disable creation of unsupported data structures
through manually crafted strings. (Dmitry)

- Zlib:
. Added zlib/level context option for compress.zlib wrapper. (Sara)

<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>
36 changes: 36 additions & 0 deletions ext/zlib/tests/zlib_wrapper_level.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
compress.zlib:// wrapper with compression level
--SKIPIF--
<?php in_array('compress.zlib', stream_get_wrappers()) || print 'skip No zlib wrapper';
--FILE--
<?php declare(strict_types=1);

$filename = tempnam(sys_get_temp_dir(), "php-zlib-test-");
$thisfile = file_get_contents(__FILE__);

function write_at_level(int $level) {
global $filename, $thisfile;

$ctx = stream_context_create(['zlib' => ['level' => $level] ]);
$fp = fopen("compress.zlib://$filename", 'w', false, $ctx);
for ($i = 0; $i < 10; ++$i) {
fwrite($fp, $thisfile);
}
fclose($fp);
$size = filesize($filename);
unlink($filename);
return $size;
}

$size1 = write_at_level(1);
$size9 = write_at_level(9);

var_dump(10 * strlen($thisfile));
var_dump($size1);
var_dump($size9);
var_dump($size9 < $size1);
--EXPECTF--
int(%d)
int(%d)
int(%d)
bool(true)
5 changes: 5 additions & 0 deletions ext/zlib/zlib_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, con
self->gz_file = gzdopen(dup(fd), mode);

if (self->gz_file) {
zval *zlevel = context ? php_stream_context_get_option(context, "zlib", "level") : NULL;
if (zlevel && (Z_OK != gzsetparams(self->gz_file, zval_get_long(zlevel), Z_DEFAULT_STRATEGY))) {
php_error(E_WARNING, "failed setting compression level");
}

stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
if (stream) {
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
Expand Down

0 comments on commit 8f804a2

Please sign in to comment.