Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Zipstream Php Laravel Package

maennchen/zipstream-php

Stream ZIP archives on the fly in PHP without writing to disk. Fast ZIP downloads with optional HTTP headers, supports adding files from strings/paths, works with S3 and PSR-7 streams, and can output to custom callbacks.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Use Case Alignment: Perfect fit for Laravel applications requiring dynamic ZIP file generation (e.g., bulk exports, report downloads, or API responses). Eliminates disk I/O bottlenecks by streaming directly to the client.
  • Laravel Synergy: Complements Laravel’s HTTP response system (e.g., Response::stream(), Symfony StreamedResponse) and integrates seamlessly with:
    • Queued Jobs: Offload ZIP generation to background workers (e.g., Laravel Queues) without temporary files.
    • API Responses: Stream ZIPs directly in API endpoints (e.g., return $zip->finish()).
    • Storage Adapters: Works with Laravel’s filesystem (e.g., S3, local) via addFileFromPath() or PSR-7 streams.
  • Performance: Zero-disk overhead reduces latency for large files (critical for user-facing exports).

Integration Feasibility

  • Laravel Compatibility:
    • PHP 8.2+: Aligns with Laravel’s supported PHP versions (LTS: 8.2+).
    • PSR-7 Streams: Integrates with Laravel’s HTTP layer (e.g., Symfony\Component\HttpFoundation\StreamedResponse).
    • Service Container: Can be registered as a singleton/binding for dependency injection.
  • Key Integration Points:
    • Middleware: Stream ZIPs via middleware (e.g., for authenticated exports).
    • Commands: Generate ZIPs in Artisan commands (e.g., php artisan export:zip).
    • Events: Trigger ZIP generation post-model events (e.g., Model::saved()).

Technical Risk

  • Breaking Changes: Version 3.x enforces PHP 8.1+ and deprecates internal APIs. Mitigation:
    • Pin to 3.2.2 (latest stable) and audit usage of @internal classes.
    • Test with Laravel’s Symfony HttpFoundation (e.g., StreamedResponse compatibility).
  • Edge Cases:
    • Large Files (>4GB): Zip64 is auto-enabled; validate with Laravel’s file limits (e.g., ini_get('memory_limit')).
    • Stream Corruption: Use CallbackStreamWrapper for custom sinks (e.g., S3) to avoid buffering issues.
    • Concurrency: Thread-safe for Laravel’s request lifecycle but test in queue workers.
  • Dependencies: No external PHP extensions required (pure PHP).

Key Questions

  1. Performance Benchmarks:
    • How does streaming compare to Laravel’s ZipArchive (disk-based) for files >1GB?
    • Action: Benchmark with microtime(true) in a Laravel test suite.
  2. Error Handling:
    • How to gracefully handle stream failures (e.g., client disconnects)?
    • Action: Wrap ZipStream in a try-catch and return a fallback (e.g., partial ZIP or error page).
  3. Security:
    • Sanitize filenames to prevent ZIP slip vulnerabilities (e.g., ../malicious.zip).
    • Action: Validate paths with Str::of($filename)->startsWith('safe/').
  4. Laravel-Specific:
    • Can ZipStream leverage Laravel’s Filesystem or Storage facades for file sources?
    • Action: Extend addFileFromPath() to accept Storage::disk()->path().

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • HTTP Layer: Replace return response()->download($path) with ZipStream for dynamic content.
    • Queues: Use ZipStream in queued jobs (e.g., HandleExportJob) to avoid blocking requests.
    • APIs: Stream ZIPs in Route::get('/export', function() { ... }) without temporary files.
  • Third-Party Integrations:
    • S3/Cloud Storage: Use CallbackStreamWrapper to pipe ZIPs directly to AWS S3 (example in release 3.2.2).
    • Mail Attachments: Stream ZIPs into SwiftMailer’s EmbeddedFile or StreamedFile.

Migration Path

  1. Phase 1: Pilot Feature
    • Replace a single disk-based ZIP endpoint (e.g., /reports/export) with ZipStream.
    • Example:
      // Before (disk-based)
      return response()->download(storage_path('reports.zip'));
      
      // After (streaming)
      $zip = new ZipStream\ZipStream('reports.zip', sendHttpHeaders: true);
      $zip->addFile('data.csv', $csvData);
      return $zip->finish();
      
  2. Phase 2: Queue Integration
    • Move ZIP generation to a queue job (e.g., ExportZipJob) with ZipStream in handle().
    • Example:
      public function handle() {
          $zip = new ZipStream\ZipStream('export.zip');
          $zip->addFileFromPath('file.txt', storage_path('temp/file.txt'));
          return $zip->finish(); // Stream to client via queue worker
      }
      
  3. Phase 3: Full Replacement
    • Audit all ZipArchive usages and replace with ZipStream where disk I/O is a bottleneck.

Compatibility

  • Laravel Versions:
    • Tested with Laravel 9+ (PHP 8.2+) due to ZipStream’s PHP 8.1+ requirement.
    • Note: Laravel 8.x may need PHP 8.1+ updates or a ZipStream fork.
  • PSR Standards:
    • Compatible with PSR-7 (GuzzleHttp\Psr7\Stream) and PSR-15 middleware.
    • Action: Use Symfony\Component\HttpFoundation\StreamedResponse for Laravel HTTP responses.

Sequencing

  1. Dependency Setup:
    • Add to composer.json:
      "require": {
          "maennchen/zipstream-php": "^3.2"
      }
      
    • Run composer update.
  2. Configuration:
    • Set defaultDeflateLevel in ZipStream constructor for consistent compression.
    • Example:
      $zip = new ZipStream\ZipStream(
          outputName: 'export.zip',
          sendHttpHeaders: true,
          defaultDeflateLevel: 6 // Balance speed/compression
      );
      
  3. Testing:
    • Unit tests for ZipStream logic (e.g., file addition, compression).
    • Integration tests with Laravel’s HTTP client to validate streaming.

Operational Impact

Maintenance

  • Pros:
    • No Disk Cleanup: Eliminates temp file management (e.g., Storage::delete()).
    • Active Development: Regular updates (last release: 2026-04-11) with security fixes (e.g., CI improvements in 3.1.1).
  • Cons:
    • API Changes: Version 3.x’s breaking changes require codebase audits.
    • Mitigation: Use ^3.2 in composer.json to auto-update patches.
  • Monitoring:
    • Log stream failures (e.g., ZipStream\Exception\StreamException) via Laravel’s App\Exceptions\Handler.

Support

  • Troubleshooting:
    • Common Issues:
      • Client Disconnects: Use StreamedResponse with timeout parameter.
      • Memory Limits: Increase memory_limit for large ZIPs or chunk data.
    • Debugging: Enable ZipStream’s debug mode (if available) or log raw stream data.
  • Community:

Scaling

  • Horizontal Scaling:
    • Stateless streaming works in load-balanced Laravel deployments (e.g., Forge/Vagrant).
    • Caveat: Avoid stateful ZipStream instances in shared-nothing architectures.
  • Vertical Scaling:
    • Memory usage scales with ZIP size; test with php -d memory_limit=512M.
    • Optimization: Use lower deflateLevel for large files (e.g., 3 instead of 9).
  • Queue Workers:
    • Offload ZIP generation to queue workers (e.g., supervisor) to free up web servers.

Failure Modes

Failure Scenario Impact Mitigation
Client disconnects mid-stream Partial ZIP or timeout Implement retry logic or fallback to disk-based ZIP.
Memory exhaustion Worker crash Increase memory_limit or chunk data into smaller ZIPs.
Cor
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai