motekar / laravel-zip
Create and Manage Zip Archives in Laravel
Requires
- php: ^8.0
- ext-zip: *
- illuminate/filesystem: ^10.0||^11.0
- illuminate/support: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.17
- spatie/temporary-directory: ^2.2
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.18
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
This package is auto-updated.
Last update: 2024-12-30 13:35:23 UTC
README
This package provides a simple and intuitive way to create and manage Zip archives in Laravel applications. It wraps PHP's ZipArchive class with additional convenience methods and fluent syntax.
Quick Example
use Motekar\LaravelZip\Facades\Zip; Zip::make(storage_path('images.zip')) ->add(glob(storage_path('images/*'))) ->close();
This code creates a zip file named images.zip
in the storage directory, containing all files from the storage/images/
folder.
Installation
Install the package via Composer:
composer require motekar/laravel-zip
Usage
use Motekar\LaravelZip\Facades\Zip; use Motekar\LaravelZip\ZipManager; // Create a zip file and add files $zip = Zip::make('test.zip') ->folder('test') ->add('composer.json'); // Add a file with a specific name $zip = Zip::make('test.zip') ->folder('test') ->add('composer.json', 'test'); // Remove a file from the archive $zip->remove('composer.lock'); // Add multiple files to a specific folder $zip->folder('mySuperPackage')->add([ 'vendor', 'composer.json' ]); // Get file content from the archive $content = $zip->getFileContent('mySuperPackage/composer.json'); // Extract specific files using whitelist $zip->make('test.zip') ->extractTo('', ['mySuperPackage/composer.json'], ZipManager::WHITELIST) ->close();
Important Notes:
- Always call
->close()
at the end to write changes to disk - Most methods are chainable except:
getFileContent
getStatus
close
extractTo
API Reference
make($pathToFile)
Creates or opens a zip archive. If the file doesn't exist, it creates a new one. Returns the ZipManager instance for method chaining.
add($filesOrFolder)
Adds files or folders to the archive. Accepts:
- An array of file paths
- A single folder path (all files in the folder will be added)
addString($filename, $content)
Adds a file to the archive using string content.
remove($files)
Removes files from the archive. Accepts:
- A single file path
- An array of file paths
folder($folder)
Sets the working folder for subsequent operations.
listFiles($regexFilter = null)
Lists files in the archive. Optionally filters files using a regex pattern.
Note: Ignores the folder set with folder()
Examples:
// Get all .log files $logFiles = Zip::make('test.zip')->listFiles('/\.log$/i'); // Get all non-.log files $notLogFiles = Zip::make('test.zip')->listFiles('/^(?!.*\.log).*$/i');
home()
Resets the folder pointer to the root.
getFileContent($filePath)
Returns the content of a file from the archive or false if not found.
getStatus()
Returns the opening status of the zip archive as an integer.
close()
Writes all changes and closes the archive.
extractTo($path, $files = [], $flags = 0)
Extracts archive contents to the specified path. Supports:
- ZipManager::WHITELIST: Extract only specified files
- ZipManager::BLACKLIST: Extract all except specified files
- ZipManager::EXACT_MATCH: Match file names exactly
Examples:
use Motekar\LaravelZip\Facades\Zip; use Motekar\LaravelZip\ZipManager; // Whitelist example Zip::make('test.zip') ->extractTo('public', ['vendor'], ZipManager::WHITELIST); // Blacklist example Zip::make('test.zip') ->extractTo('public', ['vendor'], ZipManager::BLACKLIST); // Exact match example Zip::make('test.zip') ->folder('vendor') ->extractTo('public', ['composer', 'bin/phpunit'], ZipManager::WHITELIST | ZipManager::EXACT_MATCH);
extractMatchingRegex($path, $regex)
Extracts files matching a regular expression pattern.
Examples:
// Extract PHP files Zip::make('test.zip') ->folder('src') ->extractMatchingRegex($path, '/\.php$/i'); // Exclude test files Zip::make('test.zip') ->folder('src') ->extractMatchingRegex($path, '/^(?!.*test\.php).*$/i');
Important Notes:
- PHP's ZipArchive uses '/' as the directory separator
- Windows users should use '/' in patterns instead of ''
Credits
License
This package is open-source software licensed under the Apache Version 2.0 license.