PHPackages                             selective/zip-responder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. selective/zip-responder

ActiveLibrary[File &amp; Storage](/categories/file-storage)

selective/zip-responder
=======================

A ZIP file and a stream responder (PSR-7)

0.4.0(2y ago)1017.9k↑14.4%2MITPHPPHP ^7.3 || ^8.0

Since Jan 2Pushed 2y ago2 watchersCompare

[ Source](https://github.com/selective-php/zip-responder)[ Packagist](https://packagist.org/packages/selective/zip-responder)[ Docs](https://github.com/selective-php/zip-responder)[ RSS](/packages/selective-zip-responder/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (8)Versions (5)Used By (0)

selective/zip-responder
=======================

[](#selectivezip-responder)

A ZIP responder (PSR-7).

[![Latest Version on Packagist](https://camo.githubusercontent.com/e228c7c4e88311389f34e316363c6ba4b0c75d16b85a21741619e51d71c9b80b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73656c6563746976652d7068702f7a69702d726573706f6e6465722e737667)](https://packagist.org/packages/selective/zip-responder)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)[![Build Status](https://github.com/selective-php/zip-responder/workflows/build/badge.svg)](https://github.com/selective-php/zip-responder/actions)[![Coverage Status](https://camo.githubusercontent.com/a978700dc0bd0f5e3305b6df1244f0a8f05efc56430e3fdf3f3efbe347ac4112/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f73656c6563746976652d7068702f7a69702d726573706f6e6465722e737667)](https://scrutinizer-ci.com/g/selective-php/zip-responder/code-structure)[![Quality Score](https://camo.githubusercontent.com/20934228ff2e47c9eefe01c67c0bb6a7b25467191b1410da8ce37d9e7aec9a8e/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f73656c6563746976652d7068702f7a69702d726573706f6e6465722e737667)](https://scrutinizer-ci.com/g/selective-php/zip-responder/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/ba75e3509b7422c6b85608d94a8eb3bb00a62d8762c48526cfcee3e57a9c0a46/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73656c6563746976652f7a69702d726573706f6e6465722e737667)](https://packagist.org/packages/selective/zip-responder/stats)

Table of Contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
    - [Sending a ZIP file](#sending-a-zip-file)
    - [Sending a ZIP file from a string](#sending-a-zip-file-from-a-string)
    - [Sending a ZIP stream](#sending-a-zip-stream)
    - [Sending a ZipArchive file](#sending-a-ziparchive-file)
    - [Sending a ZipStream-PHP archive](#sending-a-zipstream-php-archive)
    - [Sending a PhpZip archive](#sending-a-phpzip-archive)
- [Slim 4 Integration](#slim-4-integration)

Requirements
------------

[](#requirements)

- PHP 7.3+ or 8.0+
- A PSR-7 StreamFactory implementation, e.g. [nyholm/psr7](https://github.com/Nyholm/psr7)

Installation
------------

[](#installation)

```
composer require selective/zip-responder

```

Usage
-----

[](#usage)

Creating a new ZipResponder instance using the `nyholm/psr7` Psr17Factory:

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

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

Creating a new ZipResponder instance using the `slim/psr7` StreamFactory:

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

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

### Sending a ZIP file

[](#sending-a-zip-file)

Send ZIP file to browser, force direct download:

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

### Sending a ZIP file from a string

[](#sending-a-zip-file-from-a-string)

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

### Sending a ZIP stream

[](#sending-a-zip-stream)

Send ZIP stream to the browser, force direct download:

```
$stream = fopen('test.zip', 'r');

return $zipResponder->withZipStream($response, $stream, 'output.zip');
```

### Sending a ZIP stream on the fly

[](#sending-a-zip-stream-on-the-fly)

Sending a file directly to the client is not intended according to the PSR-7 specification, but can still be realized with the help of a CallbackStream.

```
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);
```

### Sending a ZipArchive file

[](#sending-a-ziparchive-file)

The ZIP extension enables you to transparently read or write ZIP compressed archives and the files inside them. A [ZipArchive](https://www.php.net/manual/en/class.ziparchive.php) does not support "memory mapped files", like PHP streams. You can only access local files with ZipArchive. For this purpose, you can create a temporary file, or you can use an existing file from the filesystem.

```
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');
```

### Sending a ZipStream-PHP archive

[](#sending-a-zipstream-php-archive)

[ZipStream-PHP](https://github.com/maennchen/ZipStream-PHP) is a library for streaming dynamic ZIP files without writing to the disk. You can send the file directly to the user, which is much faster and improves testability.

**Installation:**

```
composer require maennchen/zipstream-php

```

Creating and sending a ZIP file (only in-memory) to the browser:

```
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');
```

Sending a ZIP-stream on the fly:

```
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);
```

### Sending a PhpZip archive

[](#sending-a-phpzip-archive)

[PhpZip](https://github.com/Ne-Lexa/php-zip) is a library for extended work with ZIP-archives.

**Installation:**

```
composer require nelexa/zip

```

Note, when you use the `nelexa/zip` component, you may not need the `selective/zip-responder`component because the `nelexa/zip` already provides its own PSR-7 responder.

**Example**

```
use PhpZip\ZipFile;

// ...

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

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

In case you want to keep your architecture more clean (SRP), you may use the `selective/zip-responder` responder to create and send a ZIP file to the browser as follows:

```
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');
```

Slim 4 Integration
------------------

[](#slim-4-integration)

Create a DI container definition for: `StreamFactoryInterface::class` and `ZipResponder::class`

A `nyholm/psr7` and PHP-DI example:

```
