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

Provide example when reading JSON from a zip archive #114

Open
tacman opened this issue Sep 27, 2024 · 1 comment
Open

Provide example when reading JSON from a zip archive #114

tacman opened this issue Sep 27, 2024 · 1 comment

Comments

@tacman
Copy link

tacman commented Sep 27, 2024

I think it's possible to use this library when reading directly from a zip file, but I'm not quite sure how. Can you provide an example under the streaming section please?

And again, thanks for this awesome library.

@halaxa
Copy link
Owner

halaxa commented Nov 22, 2024

I'm glad it helps. Just tried asking ChatGPT as I don't have much time to fiddle with it:

use JsonMachine\Items;

$zipFilePath = 'path/to/archive.zip';
$jsonFileInZip = 'data.json';

$zip = new ZipArchive();
if ($zip->open($zipFilePath) === TRUE) {
    $stream = $zip->getStream($jsonFileInZip);
    if ($stream) {
        $jsonItems = Items::fromStream($stream);
        foreach ($jsonItems as $key => $value) {
            echo "Key: $key, Value: ";
            print_r($value);
            echo PHP_EOL;
        }
        fclose($stream);
    } else {
        echo "The JSON file $jsonFileInZip was not found in the ZIP archive.";
    }
    $zip->close();
} else {
    echo "Failed to open the ZIP archive.";
}

Seems ok to me. Should work somewhere along those lines. Can you please give feedback here if it works?

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