- How do I integrate ZipStream-PHP into a Laravel controller to send a ZIP download directly to the browser?
- Use Laravel’s `Response` class with `streamDownload()`. Initialize `ZipStream`, add files, then pass the stream to `Response::stream()`. Example: `$response = Response::streamDownload(fn() => $zip->getStream(), 'export.zip');` Ensure `sendHttpHeaders: true` in `ZipStream` for proper browser handling.
- Can ZipStream-PHP handle large files (e.g., 1GB+) without memory issues?
- Yes, it streams files directly without buffering the entire ZIP in memory. For files >4GB, Zip64 is auto-enabled, but test directory structures >4GB (fixed in v3.2.2). Monitor memory usage with `memory_get_usage()` if processing edge cases.
- Does ZipStream-PHP work with Laravel 9.x or only Laravel 10+?
- ZipStream-PHP requires PHP 8.2+, which aligns with Laravel 10+. For Laravel 9.x (PHP 8.1), use a fork like `maennchen/zipstream-php:dev` or polyfill missing features. Test thoroughly for compatibility.
- How can I stream a ZIP directly to AWS S3 instead of the browser?
- Use `S3StreamWrapper` from the package. Initialize `ZipStream` with `outputStream: S3StreamWrapper::open($s3Client, 'bucket', 'file.zip')` and set `sendHttpHeaders: false`. The ZIP will stream directly to S3 without temporary files.
- What’s the best way to add dynamic content (e.g., database-generated files) to a ZIP?
- Use `addFile()` with string data or `addFileFromPath()` for filesystem files. For dynamic content, generate files in memory (e.g., PDFs via Dompdf) and pass the binary string. Example: `$zip->addFile('report.pdf', $pdf->output());`
- How do I handle errors if the ZIP stream fails (e.g., S3 upload errors)?
- Wrap `ZipStream` in a try-catch block. For partial failures, log errors and either abort the stream (throw an exception) or save the partial ZIP to a fallback location. Use Laravel’s `Log::error()` for debugging.
- Is ZipStream-PHP compatible with Laravel’s queue system for background ZIP generation?
- Yes. Dispatch a job (e.g., `HandleZipExportJob`) that generates the ZIP, then stream it to the user upon job completion. Use `Response::streamDownload()` in the job’s handle method or store the ZIP in S3 and send a download link.
- Can I add progress tracking for large ZIP downloads in the browser?
- Yes. Use `Content-Length` headers to enable progress bars. For Nginx, add `X-Accel-Buffering: no` to the response. Alternatively, implement client-side polling with a `progress` endpoint that tracks streamed bytes.
- What are the alternatives to ZipStream-PHP for Laravel ZIP generation?
- For small ZIPs (<100MB), Laravel’s built-in `ZipArchive` suffices. For S3-heavy workflows, combine `spatie/laravel-activitylog` with S3 events. ZipStream-PHP excels for large, dynamic, or streaming use cases due to its memory efficiency.
- How do I test ZipStream-PHP in a Laravel application before production?
- Test with small ZIPs first (e.g., 10–50MB) to verify streaming. Use Laravel’s `Http::fake()` to mock responses, then scale to larger files. Validate S3 streams with `Storage::fake('s3')` and check for edge cases like 100K+ files or Unicode filenames.