9951 explained code solutions for 126 technologies


php-laravelHow can I use the PHP Zipstream library in a Laravel project?


The PHP Zipstream library can be used in a Laravel project to create and stream archives on-the-fly. It is a lightweight alternative to the ZipArchive class for creating ZIP files.

To begin, you'll need to install the library using Composer.

composer require grandt/phpzip

Once the library is installed, you can use it in your Laravel project. For example, to create a ZIP file containing files from the storage/app/public directory:

use Grandt\PhpZip\ZipFile;

$zip = new ZipFile();
$zip->addDirectoryContent('storage/app/public');
$zip->outputAsAttachment('files.zip');

The output of the code above would be a ZIP file called files.zip containing the contents of the storage/app/public directory.

You can also add files to the archive from other sources. For example, to add a file from an URL:

$zip->addFileFromUrl('http://example.com/file.txt', 'file.txt');

You can find more information about the library and its features in the PHP Zipstream library documentation.

Edit this code on GitHub