PHP code example of maennchen / zipstream-php

1. Go to this page and download the library: Download maennchen/zipstream-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maennchen / zipstream-php example snippets


// Autoload the dependencies
bject
$zip = new ZipStream\ZipStream(
    outputName: 'example.zip',

    // enable output of HTTP headers
    sendHttpHeaders: true,
);

// create a file named 'hello.txt'
$zip->addFile(
    fileName: 'hello.txt',
    data: 'This is the contents of hello.txt',
);

// add a file named 'some_image.jpg' from a local file 'path/to/image.jpg'
$zip->addFileFromPath(
    fileName: 'some_image.jpg',
    path: 'path/to/image.jpg',
);

// finish the zip stream
$zip->finish();

use ZipStream\ZipStream;
use ZipStream\Stream\CallbackStreamWrapper;

// Stream to a callback function with proper file handling
$outputFile = fopen('output.zip', 'wb');
$backupFile = fopen('backup.zip', 'wb');

$zip = new ZipStream(
    outputStream: CallbackStreamWrapper::open(function (string $data) use ($outputFile, $backupFile) {
        // Handle ZIP data as it's generated
        fwrite($outputFile, $data);
        
        // Send to multiple destinations efficiently
        echo $data; // Browser
        fwrite($backupFile, $data); // Backup file
    }),
    sendHttpHeaders: false,
);

$zip->addFile('hello.txt', 'Hello World!');
$zip->finish();

// Clean up resources
fclose($outputFile);
fclose($backupFile);
bash
composer