Skip to content

Commit 136f51f

Browse files
committed
Fix #76584: PharFileInfo::decompress not working
We actually have to decompress, when told to do so.
1 parent fd08f06 commit 136f51f

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ PHP NEWS
2222
- OpenSSL:
2323
. Fixed bug #79145 (openssl memory leak). (cmb, Nikita)
2424

25+
- Phar:
26+
. Fixed bug #76584 (PharFileInfo::decompress not working). (cmb)
27+
2528
- Reflection:
2629
. Fixed bug #79115 (ReflectionClass::isCloneable call reflected class
2730
__destruct). (Nikita)

ext/phar/phar_object.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5011,6 +5011,7 @@ PHP_METHOD(PharFileInfo, compress)
50115011
PHP_METHOD(PharFileInfo, decompress)
50125012
{
50135013
char *error;
5014+
char *compression_type;
50145015
PHAR_ENTRY_OBJECT();
50155016

50165017
if (zend_parse_parameters_none() == FAILURE) {
@@ -5061,12 +5062,24 @@ PHP_METHOD(PharFileInfo, decompress)
50615062
/* re-populate after copy-on-write */
50625063
entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
50635064
}
5064-
if (!entry_obj->entry->fp) {
5065-
if (FAILURE == phar_open_archive_fp(entry_obj->entry->phar)) {
5066-
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot decompress entry \"%s\", phar error: Cannot open phar archive \"%s\" for reading", entry_obj->entry->filename, entry_obj->entry->phar->fname);
5065+
switch (entry_obj->entry->flags & PHAR_ENT_COMPRESSION_MASK) {
5066+
case PHAR_ENT_COMPRESSED_GZ:
5067+
compression_type = "gzip";
5068+
break;
5069+
case PHAR_ENT_COMPRESSED_BZ2:
5070+
compression_type = "bz2";
5071+
break;
5072+
default:
5073+
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5074+
"Cannot decompress file compressed with unknown compression type");
50675075
return;
5068-
}
5069-
entry_obj->entry->fp_type = PHAR_FP;
5076+
}
5077+
/* decompress this file indirectly */
5078+
if (SUCCESS != phar_open_entry_fp(entry_obj->entry, &error, 1)) {
5079+
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5080+
"Phar error: Cannot decompress %s-compressed file \"%s\" in phar \"%s\": %s", compression_type, entry_obj->entry->filename, entry_obj->entry->phar->fname, error);
5081+
efree(error);
5082+
return;
50705083
}
50715084

50725085
entry_obj->entry->old_flags = entry_obj->entry->flags;

ext/phar/tests/bug76584.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Bug #76584 (PharFileInfo::decompress not working)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('phar')) die('skip phar extension not available');
6+
if (!extension_loaded('zlib')) die('skip zlib extension not available');
7+
?>
8+
--INI--
9+
phar.readonly=0
10+
--FILE--
11+
<?php
12+
$phar = new Phar(__DIR__ . '/76584.phar');
13+
$phar->addFromString('76584.txt', 'This is a test file.');
14+
$file = $phar['76584.txt'];
15+
var_dump($file->compress(Phar::GZ));
16+
var_dump($file->isCompressed());
17+
var_dump($file->decompress());
18+
var_dump($file->isCompressed());
19+
mkdir(__DIR__ . '/76584');
20+
var_dump($phar->extractTo(__DIR__ . '/76584'));
21+
echo file_get_contents(__DIR__ . '/76584/76584.txt');
22+
?>
23+
--EXPECT--
24+
bool(true)
25+
bool(true)
26+
bool(true)
27+
bool(false)
28+
bool(true)
29+
This is a test file.
30+
--CLEAN--
31+
<?php
32+
unlink(__DIR__ . '/76584/76584.txt');
33+
rmdir(__DIR__ . '/76584');
34+
unlink(__DIR__ . '/76584.phar');
35+
?>

0 commit comments

Comments
 (0)