Skip to content
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

Extract files to stream #88

Open
mstimvol opened this issue Mar 13, 2022 · 1 comment
Open

Extract files to stream #88

mstimvol opened this issue Mar 13, 2022 · 1 comment

Comments

@mstimvol
Copy link

Description
It would be great to have a function to extract files to a stream instead of a directory. At the moment, I'm calling openFromStream to read my zip archive and then calling extractTo to extract all the files to a temporary directory. Next, I'm using a foreach-loop to iterate through the files and send them back to another stream. So writing / extracting them to the local disk is completely unnecessary since I want to read the zip archive from a stream, use a foreach loop to iterate over all entries and extract each file to a stream.

Example

$zipfile = new \PhpZip\ZipFile();
$zipfile->openFromStream($myRemoteStream);

foreach ($zipfile as $entryName) {
    $stream = openStream();
    // Extract the content of $entryName to the given stream...
    $zipfile->extractTo($stream, $entryName);
    close($stream);
}

$zipfile->close();
@odan
Copy link
Contributor

odan commented Aug 30, 2023

You can use the getEntryStream method for this.

Example:

// This is optional: Create a ZIP file as stream
$zipStream = fopen('php://temp', 'ab');
fwrite($zipStream, base64_decode('UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA=='));

$zip = new \PhpZip\ZipFile();
$zip->openFromStream($zipStream);

$zip->addFromString('example.txt', 'my content');
$zip->addFromString('sub1/example2.txt', 'my content2');

$listFiles = $zip->getListFiles();
foreach ($listFiles as $entryName) {
    //  Extract the content of $entryName to the given stream
    $stream = $zip->getEntryStream($entryName);

    // Optional: Convert stream to string
    $content = stream_get_contents($stream);

    fclose($stream);
}

$zip->close();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants