exeque/laravel-zipstream 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

exeque/laravel-zipstream

最新稳定版本:0.1.2

Composer 安装命令:

composer require exeque/laravel-zipstream

包简介

Zip streaming for Laravel

README 文档

README

Laravel ZipStream

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

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.

统计信息

  • 总下载量: 74
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固