Installation:
composer require zanysoft/laravel-zip
Add the service provider to config/app.php:
ZanySoft\Zip\ZipServiceProvider::class,
Optionally, register the facade for convenience:
'Zip' => ZanySoft\Zip\Facades\Zip::class,
First Use Case: Create a zip file and add a file to it:
use Zip;
$zip = Zip::open('path/to/output.zip', 'w');
$zip->add('path/to/source/file.txt');
$zip->close();
Zip::open() to start working with zip files.add(), addFiles(), extract(), and close().$zip = Zip::open('backup.zip', 'w');
$zip->addFiles(['file1.txt', 'file2.txt', 'folder/*']);
$zip->close();
$zip = Zip::open('backup.zip', 'r');
$zip->extract('path/to/destination');
$zip->close();
$zip = Zip::open('archive.zip', 'w');
$zip->add('file.txt', 'custom/path/in/zip/file.txt');
$zip->close();
Use Zip::stream() to handle large files without loading them entirely into memory:
$zip = Zip::stream('large_archive.zip', 'w');
$zip->add('huge_file.log');
$zip->close();
Combine with Laravel’s filesystem to handle cloud storage (e.g., S3):
use Storage;
$zip = Zip::open(storage_path('app/backup.zip'), 'w');
$zip->add(Storage::disk('s3')->path('file.txt'));
$zip->close();
Use Laravel’s scheduling to run zip operations periodically:
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:create')->daily();
}
Laravel Events: Trigger zip operations on events (e.g., job.failed):
event(new JobFailed($job, $exception));
Zip::open('logs/failed_jobs.zip', 'w')->add(storage_path('logs/laravel.log'));
API Responses: Serve zip files directly in API responses:
return response()->download(storage_path('app/export.zip'))
->header('Content-Type', 'application/zip');
Queue Jobs: Offload zip creation to a queue for long-running tasks:
ZipJob::dispatch('large_archive.zip', ['file1.log', 'file2.log']);
Memory Limits:
memory_limit. Use Zip::stream() for big archives.php.ini or set ini_set('memory_limit', '512M') temporarily.File Paths:
realpath() for clarity:
$zip->add(realpath('path/to/file.txt'));
Zip Modes:
'w' (write) overwrites existing files. Use 'a' (append) to add to existing zips:
$zip = Zip::open('archive.zip', 'a');
Permissions:
storage_path() for Laravel’s storage folder.Nested Directories:
addFiles() does not recursively add directories by default. Use Zip::addDirectory() or glob patterns:
$zip->addFiles(['folder/**/*']);
Check Zip Status:
Use Zip::getStatus() to debug errors:
$zip = Zip::open('file.zip', 'w');
if ($zip->getStatus() !== true) {
throw new \RuntimeException('Failed to create zip: ' . $zip->getStatusMessage());
}
Log Errors: Enable Laravel’s logging to capture zip-related exceptions:
try {
$zip->extract('destination');
} catch (\Exception $e) {
Log::error('Zip extraction failed: ' . $e->getMessage());
}
Compression Levels: Adjust compression for balance between speed and size:
$zip = Zip::open('compressed.zip', 'w', ZipArchive::CM_DEFLATE, 9); // Max compression
Exclude Files:
Use Zip::addFiles() with a glob pattern to exclude files:
$zip->addFiles(['*.txt', '!temp/*.tmp']); // Exclude temp files
Facade vs. Class:
Prefer the facade (Zip::open()) for simplicity in controllers/blades. Use the class (new \ZanySoft\Zip\Zip()) in services for dependency injection.
Testing:
Mock zip operations in tests using Laravel’s Zip facade:
$this->partialMock(Zip::class, 'open')->willReturn($mockZip);
Custom Naming:
Use Zip::setName() to rename files when adding:
$zip->add('old_name.txt', 'new_name.txt');
Laravel Mix: Integrate with Laravel Mix for frontend asset zipping:
// mix.js
mix.js('resources/js/app.js', 'public/js')
.then(() => {
Zip::open('public/assets.zip', 'w')->add('public/js/app.js');
});
How can I help you explore Laravel packages today?