PHP code example of selective / zip-responder

1. Go to this page and download the library: Download selective/zip-responder 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/ */

    

selective / zip-responder example snippets


use Selective\Http\Zip\ZipResponder;
use Nyholm\Psr7\Factory\Psr17Factory;

$zipResponder = new ZipResponder(new Psr17Factory());

use Selective\Http\Zip\ZipResponder;
use Slim\Psr7\Factory\StreamFactory;

$zipResponder = new ZipResponder(new StreamFactory());

return $zipResponder->withZipFile($response, 'source.zip', 'output.zip');

return $zipResponder->withZipString($response, file_get_contents('example.zip'), 'output.zip');

$stream = fopen('test.zip', 'r');
 
return $zipResponder->withZipStream($response, $stream, 'output.zip');

use Selective\Http\Zip\Stream\CallbackStream;

$callbackStream = new CallbackStream(function () {
    echo 'my binary zip content';
}

$response = $zipResponder->withZipHeaders($response, $outputName, true);

return $response->withBody($callbackStream);

use ZipArchive;
// ...

// Create temporary filename
$filename = tempnam(sys_get_temp_dir(), 'zip');

// Add files to temporary ZIP file
$zip = new ZipArchive();
$zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString('test.txt', 'my content');
$zip->close();

// Render ZIP file into the response as stream
return $zipResponder->withZipStream($response, fopen($filename, 'r'), 'download.zip');

use ZipStream\ZipStream;

// ...

// Create ZIP file, only in-memory
$stream = fopen('php://memory', 'w+b');

$zip = new ZipStream(
    outputStream: $stream,
    // disable output of HTTP headers
    sendHttpHeaders: false,
);

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

$zip->finish();

$response = $zipResponder->withZipStream($response, $stream, 'download.zip');

use Selective\Http\Zip\Stream\CallbackStream;
use ZipStream\ZipStream;
//...

$callbackStream = new CallbackStream(function () {
    // Flush ZIP file directly to output stream (php://output)
    $zip = new ZipStream(
        flushOutput: true,
        sendHttpHeaders: false,
    );

    // Add files to ZIP file and stream it directly
    $zip->addFile('test.txt', 'my file content');
    $zip->addFile('test2.txt', 'my file content 2');
    $zip->addFile('test3.txt', 'my file content 4');
    $zip->finish();
});

$response = $zipResponder->withZipHeaders($response, $outputName, true);

return $response->withBody($callbackStream);

use PhpZip\ZipFile;

// ...

$zipFile = new ZipFile();
$zipFile->addFromString('test.txt', 'File content');

return $zipFile->outputAsResponse($response, 'download.zip');

use PhpZip\ZipFile;

// ...

// Create new archive
$zipFile = new ZipFile();

// Add entry from string
$zipFile->addFromString('test.txt', 'File content');
     
return $zipResponder->withZipString($response, $zipFile->outputAsString(), 'download.zip');



use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Selective\Http\Zip\ZipResponder;

return [
    // ...

    StreamFactoryInterface::class => function (ContainerInterface $container) {
        return $container->get(Psr17Factory::class);
    },

    ZipResponder::class => function (ContainerInterface $container) {
        return new ZipResponder($container->get(StreamFactoryInterface::class));
    },
];



use Psr\Container\ContainerInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Slim\Psr7\Factory\StreamFactory;
use Selective\Http\Zip\ZipResponder;

return [
    // ...
    
    StreamFactoryInterface::class => function () {
        return new StreamFactory();
    },
    
    ZipResponder::class => function (ContainerInterface $container) {
        return new ZipResponder($container->get(StreamFactoryInterface::class));
    },
];



namespace App\Action;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Selective\Http\Zip\ZipResponder;
use ZipArchive;

final class ZipDemoAction
{
    /**
     * @var ZipResponder
     */
    private $zipResponder;

    public function __construct(ZipResponder $zipResponder)
    {
        $this->zipResponder = $zipResponder;
    }

    public function __invoke(
        ServerRequestInterface $request, 
        ResponseInterface $response
    ): ResponseInterface {
        $filename = tempnam(sys_get_temp_dir(), 'zip');

        $zip = new ZipArchive();
        $zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
        $zip->addFromString('test.txt', 'my content');
        $zip->close();

        return $this->zipResponder->withZipFile($response, $filename, 'filename.zip');
    }
}

composer