-
Notifications
You must be signed in to change notification settings - Fork 8k
ext/zip: add ZipArchive::openBuffer method #14078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1483,6 +1483,47 @@ PHP_METHOD(ZipArchive, open) | |
} | ||
/* }}} */ | ||
|
||
/* {{{ Create new read-only zip using given buffer */ | ||
PHP_METHOD(ZipArchive, openBuffer) | ||
{ | ||
struct zip *intern; | ||
zend_string *buffer; | ||
zval *self = ZEND_THIS; | ||
ze_zip_object *ze_obj; | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &buffer) == FAILURE) { | ||
RETURN_THROWS(); | ||
} | ||
|
||
zip_error_t err; | ||
zip_error_init(&err); | ||
|
||
zip_source_t * zip_source = zip_source_buffer_create(ZSTR_VAL(buffer), ZSTR_LEN(buffer), 0, &err); | ||
|
||
if (!zip_source) { | ||
zend_throw_error(NULL, "Cannot open zip: %s", zip_error_strerror(&err)); | ||
zip_error_fini(&err); | ||
RETURN_THROWS(); | ||
} | ||
|
||
ze_obj = Z_ZIP_P(self); | ||
|
||
intern = zip_open_from_source(zip_source, ZIP_RDONLY, &err); | ||
if (!intern) { | ||
zip_source_free(zip_source); | ||
zend_throw_error(NULL, "Cannot open zip: %s", zip_error_strerror(&err)); | ||
|
||
zip_error_fini(&err); | ||
RETURN_THROWS(); | ||
} | ||
|
||
zip_source_keep(zip_source); | ||
zip_error_fini(&err); | ||
|
||
ze_obj->za = intern; | ||
ze_obj->source = zip_source; | ||
} | ||
/* }}} */ | ||
|
||
/* {{{ Set the password for the active archive */ | ||
PHP_METHOD(ZipArchive, setPassword) | ||
{ | ||
|
@@ -1548,12 +1589,22 @@ PHP_METHOD(ZipArchive, close) | |
ze_obj->err_sys = 0; | ||
} | ||
|
||
/* clear cache as empty zip are not created but deleted */ | ||
php_clear_stat_cache(1, ze_obj->filename, ze_obj->filename_len); | ||
// if we have a filename, we need to free it | ||
if (ze_obj->filename) { | ||
/* clear cache as empty zip are not created but deleted */ | ||
php_clear_stat_cache(1, ze_obj->filename, ze_obj->filename_len); | ||
|
||
efree(ze_obj->filename); | ||
ze_obj->filename = NULL; | ||
ze_obj->filename_len = 0; | ||
} | ||
|
||
if (ze_obj->source) { | ||
zip_source_close(ze_obj->source); | ||
zip_source_free(ze_obj->source); | ||
ze_obj->source = NULL; | ||
} | ||
|
||
efree(ze_obj->filename); | ||
ze_obj->filename = NULL; | ||
ze_obj->filename_len = 0; | ||
ze_obj->za = NULL; | ||
|
||
if (!err) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--TEST-- | ||
ZipArchive::openBuffer() method | ||
--EXTENSIONS-- | ||
zip | ||
--FILE-- | ||
<?php | ||
$zip = new ZipArchive(); | ||
$zip->openBuffer(file_get_contents(__DIR__."/test_procedural.zip")); | ||
|
||
for ($i = 0; $i < $zip->numFiles; $i++) { | ||
$stat = $zip->statIndex($i); | ||
echo $stat['name'] . "\n"; | ||
} | ||
|
||
// Zip is read-only, not allowed | ||
var_dump($zip->addFromString("foobar/baz", "baz")); | ||
var_dump($zip->addEmptyDir("blub")); | ||
|
||
var_dump($zip->close()); | ||
?> | ||
--EXPECTF-- | ||
foo | ||
bar | ||
foobar/ | ||
foobar/baz | ||
bool(false) | ||
bool(false) | ||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't be helpful to throw exceptions here instead of silent false? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we should update the behaviour of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that would be bc and require an rfc i think, but @TimWolla can maybe say here more 🙈 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this will change the behavior of already existing methods |
||
bool(true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not throw
Error
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
converted to
zend_value_error
- hope this was the correct change to do.