exeque/laravel-zipstream
Composer 安装命令:
composer require exeque/laravel-zipstream
包简介
Zip streaming for Laravel
关键字:
README 文档
README
Laravel ZipStream
A fluent Laravel wrapper for maennchen/zipstream-php to easily generate and stream ZIP archives.
Installation
You can install the package via composer:
composer require exeque/laravel-zipstream
The service provider will automatically register itself.
Basic Usage
The easiest way to use the library is via the Zip facade. You can fluently chain methods to add files and then generate a response or save the ZIP.
use ExeQue\ZipStream\Facades\Zip; return Zip::as('photos.zip') ->fromDisk('public', 'images/photo1.jpg') ->fromLocal('/path/to/local/file.pdf', 'invoice.pdf') ->fromRaw('notes.txt', 'Direct text content') ->toResponse();
Adding Content
From Laravel Disks
Add files stored on any of your configured Laravel filesystems.
Zip::fromDisk('s3', 'exports/data.csv'); // With custom destination path in ZIP Zip::fromDisk('s3', 'exports/data.csv', '2023/report.csv');
From Local Path
Add files from the local filesystem.
Zip::fromLocal('/tmp/temp-file.log'); // With custom destination path in ZIP Zip::fromLocal('/tmp/temp-file.log', 'logs/system.log');
From Raw Content
Add content directly from a string, resource, or stream.
Zip::fromRaw('hello.txt', 'Hello World');
From Custom Classes (Contracts)
You can implement StreamableToZip or CanStreamToZip on your custom classes (e.g., a Media model or MediaCollection) to easily add them to the ZIP archive.
StreamableToZip
The StreamableToZip contract is ideal for individual models that represent a file.
use ExeQue\ZipStream\Contracts\StreamableToZip; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Storage; class Media extends Model implements StreamableToZip { public function stream() { // Return resource, string, StreamInterface, or a callable that returns one of these. return Storage::disk($this->disk)->readStream($this->path); } public function destination(): string { return "{$this->collection_name}/{$this->file_name}"; } } Zip::add(Media::first());
CanStreamToZip
The CanStreamToZip contract is useful for classes that represent a collection of files, such as a MediaCollection.
use ExeQue\ZipStream\Contracts\CanStreamToZip; use Illuminate\Database\Eloquent\Collection; class MediaCollection extends Collection implements CanStreamToZip { public function getStreamableToZip(): iterable { return $this->all(); } } $media = Media::where('collection_name', 'avatars')->get(); $collection = new MediaCollection($media); Zip::add($collection);
Empty Directories
Create an empty directory within the ZIP.
Zip::emptyDirectory('backups');
Customizing Files
You can pass a callback as the last argument to any of the from* methods to customize file-specific options.
use ExeQue\ZipStream\Content\LocalFile; Zip::fromLocal('/path/file.txt', 'file.txt', function (LocalFile $file) { $file->comment('This is a important file') ->deflate() ->deflateLevel(9); });
Extending the Builder (Macros)
The Zip facade and Builder class use the Laravel Macroable trait, allowing you to add custom functionality at runtime.
use ExeQue\ZipStream\Facades\Zip; Zip::macro('fromS3', function (string $path, ?string $destination = null) { return $this->fromDisk('s3', $path, $destination); }); // Usage Zip::fromS3('exports/report.pdf')->toResponse();
Global ZIP Options
Configuration
You can publish the config file to set global defaults:
php artisan vendor:publish --tag="laravel-zipstream-config"
Available options in config/laravel-zipstream.php:
default_compression_method: "DEFLATE", "STORE", or null.default_deflate_level: 0-9.enable_zero_header: true or false.
Fluent Configuration
Customize the ZIP options for a specific archive:
Zip::as('archive.zip') ->store() // No compression ->withZeroHeader() ->fromLocal($file) ->toResponse();
Output Options
Stream to Browser (Response)
Returns a Symfony\Component\HttpFoundation\StreamedResponse.
return Zip::as('download.zip') ->fromDisk('public', 'large-file.mp4') ->toResponse();
Save to Local Path
Zip::fromRaw('test.txt', 'content') ->saveToLocal('/path/to/save/archive.zip');
Save to Laravel Disk
Zip::fromRaw('test.txt', 'content') ->saveToDisk('s3', 'backups/today.zip');
Get as String or Stream
// Get as string $content = Zip::fromRaw('a.txt', '...')->output(); // Get as PSR-7 Stream $stream = Zip::fromRaw('a.txt', '...')->output(true);
Events
Register handlers via on() to observe or react to what happens during streaming.
use ExeQue\ZipStream\Events\EventType; use ExeQue\ZipStream\Facades\Zip; Zip::as('archive.zip') ->on(EventType::ProcessStarted, fn (string $id) => Log::info("Zip $id started")) ->on([EventType::StreamingFile, EventType::StreamedFile], function ($file, $options, string $id) { // fires before/after each file }) ->fromDisk('public', 'images/photo1.jpg') ->toResponse();
EventType::Any matches every event. Available types: ProcessStarted, ProcessFinished, ProcessAborted, ProcessError, StreamingDirectory/StreamedDirectory, StreamingFile/StreamedFile, StreamingToZip/StreamedToZip, SavingToDisk/SavedToDisk, SavingToFilesystem/SavedToFilesystem, StreamingResponse/StreamedResponse, Any.
Handling Errors
If streaming an entry throws (e.g. a file that disappeared on disk between verification and streaming), the exception is passed to any handler registered for ProcessError. If no handler is registered, the exception is simply thrown. A handler is responsible for re-throwing if it wants processing to stop; otherwise, processing continues with the next entry.
use ExeQue\ZipStream\Events\EventType; use ExeQue\ZipStream\Exceptions\FileUnavailableException; use Throwable; Zip::as('archive.zip') ->on(EventType::ProcessError, function (Throwable $e, string $id) { if (!$e instanceof FileUnavailableException) { throw $e; // abort on anything unexpected } report($e); // log and skip the missing file }) ->fromDisk('public', 'images/photo1.jpg') ->toResponse();
DiskFile::stream() and LocalFile::stream() throw FileUnavailableException (carrying the failing $entry) if the underlying disk/filesystem fails to open a read stream, even after passing verification.
Testing
The package includes a comprehensive test suite. You can run the tests using Pest:
composer test
License
The MIT License (MIT). Please see License File for more information.
exeque/laravel-zipstream 适用场景与选型建议
exeque/laravel-zipstream 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74 次下载、GitHub Stars 达 10, 最近一次更新时间为 2026 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「assert」 「zip」 「zipstream」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 exeque/laravel-zipstream 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 exeque/laravel-zipstream 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 exeque/laravel-zipstream 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Allow packages to be installed from a repository or tarball bundle that have multiple packages in the sub-folders
Convenient array-related routine & better type casting
Iteration tools for PHP
Convert and operate with FIPS codes for states, counties, etc.
A tiny wrapper around webmozart/assert that is easily extendable to throw project-specific assertions.
Smart compressed files extractor
统计信息
- 总下载量: 74
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-25
