motekar/laravel-zip
Composer 安装命令:
composer require motekar/laravel-zip
包简介
Create and Manage Zip Archives in Laravel
README 文档
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:
getFileContentgetStatuscloseextractTo
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.
motekar/laravel-zip 适用场景与选型建议
motekar/laravel-zip 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 220 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「zip」 「archive」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 motekar/laravel-zip 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 motekar/laravel-zip 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 motekar/laravel-zip 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Allow packages to be installed from a repository or tarball bundle that have multiple packages in the sub-folders
Iteration tools for PHP
Convert and operate with FIPS codes for states, counties, etc.
A trait to make an Eloquent model archivable
Smart compressed files extractor
OriginPHP Zip
统计信息
- 总下载量: 220
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2024-12-28