Skip to content

Conversation

shyim
Copy link

@shyim shyim commented Apr 29, 2024

Allow opening read-only zips by string.

An example use-case would be to download a zip file and without saving it to disk, to open it and get one file out of it.

See also #11594

It's my first PR, I run the test with also Valgrind. Did I miss something 🤔?

@michaljusiega
Copy link

Hi php-team! It's possible to push this new feature into PHP-8.4 ?

Thanks for reply!

@shyim shyim requested a review from kocsismate June 22, 2024 06:46
@shyim shyim force-pushed the add-ziparchive-openbuffer branch from ccd3914 to db22fee Compare July 30, 2025 14:06
@shyim shyim force-pushed the add-ziparchive-openbuffer branch from db22fee to 87f690a Compare July 30, 2025 14:06
@shyim shyim requested a review from kocsismate July 30, 2025 14:09
@Gaitholabi
Copy link

Hello,
Is there anything missing on this PR, or any changes you’d like us to make, to get this ready for merging?

Appreciate your time!

@Girgias Girgias requested a review from a team September 3, 2025 10:05
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not throw Error.

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.

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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment on lines +26 to +27
bool(false)
bool(false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be helpful to throw exceptions here instead of silent false?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should update the behaviour of addFromString/addEmptyDir to throw exceptions 🤔

Copy link
Author

Choose a reason for hiding this comment

The 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 🙈

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this will change the behavior of already existing methods

@remicollet
Copy link
Member

remicollet commented Sep 4, 2025

I'm not a big fan of this addition, which may raise huge memory needs (archives are usually big)

I don't like the exception (value error), which makes this method inconsistent with the standard open

I don't see what warranty the buffer will be available after the method call ?
The string may need to be protected (addref) and released during the close.
(yes valgrind doesn't detect any problem)

Also, if accepted, need to be applied to https://github.com/pierrejoye/php_zip

@Gaitholabi
Copy link

I'm not a big fan of this addition, which may raise huge memory needs (archives are usually big)

The memory footprint here is indeed an issue, but I think there is still merit in having such functionality. In our usecase, we process small archives that we already have in memory but the current interface forces us to write them into disk then reread.

In my opinion, we can try to mitigate this with few things:

  • clearly document that this is intended for small archives, and open is the preferred method for large archives
  • heuristically deny large buffers - maybe we could compare against the memory_limit, this won't be fool proof
  • add a force flag to ignore the protection against large buffers

I don't like the exception (value error), which makes this method inconsistent with the standard open

I could align the returns to be consistent and not throw errors.

I don't see what warranty the buffer will be available after the method call ? The string may need to be protected (addref) and released during the close. (yes valgrind doesn't detect any problem)

I will see what I can do to protect the buffer lifetime.

Also, if accepted, need to be applied to https://github.com/pierrejoye/php_zip

If you agree with these changes, I’ll go ahead and prepare them.

Copy link
Member

@DanielEScherzer DanielEScherzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since RM review was requested: we feel that this is not ready yet to include in 8.5 given the existing discussion, and trying to get it in before Tuesday would rush things. Marking as request changes to make it clear this should not merge until after 8.5 is branched

@UlrichThomasGabor
Copy link

I'm not a big fan of this addition, which may raise huge memory needs (archives are usually big)

I was missing exactly that functionality around the time when the PR was opened. Functionality can always be used wrongly – that is in the hand of the developer. But, without this functionality, the only alternative to create in-memory archives is to utilize a ramdisk and/or hassle with file system accesses, adding unnecessary complexity, just to create an archive of 2 MB in size or whatever on machines which have hundreds of GB of RAM. In my opinion there is no loss in adding this functionality, but there are cases where exactly that functionality is the best solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants