madnest/madzipper
Composer 安装命令:
composer require madnest/madzipper
包简介
Easier zip file handling for Laravel applications.
关键字:
README 文档
README
This is a simple Wrapper around the ZipArchive methods with some handy functions.
Installation
madnest/madzippercan be installed by running:
composer require madnest/madzipper
- Optionally when using with Laravel, go to
app/config/app.php
- add to providers
Madnest\Madzipper\MadzipperServiceProvider::class
You can now access Madzipper with the Madzipper alias.
Versions
| Package | Laravel | PHP |
|---|---|---|
| v1.7 | 12.* 13.* |
^8.3 |
| v1.6 | 11.* 12.* |
^8.2 |
| v1.5 | 11.* |
^8.2 |
| v1.4 | 10.* |
^8.1 |
| v1.3 | 9.* |
^8.0 |
| v1.2 | 8.* |
^8.0 |
Simple Laravel example by using Madzipper facade
$files = glob('public/files/*'); Madzipper::make('public/test.zip')->add($files)->close();
- by default the package will create the
test.zipin the project route folder but in the example above we changed it toproject_route/public/.
Another example
$zipper = new \Madnest\Madzipper\Madzipper; $zipper->make('test.zip')->folder('test')->add('composer.json'); $zipper->zip('test.zip')->folder('test')->add('composer.json','test'); $zipper->remove('composer.lock'); $zipper->folder('mySuperPackage')->add( array( 'vendor', 'composer.json' ), ); $zipper->getFileContent('mySuperPackage/composer.json'); $zipper->make('test.zip')->extractTo('', ['mySuperPackage/composer.json'], Madzipper::WHITELIST); $zipper->close();
Note: Please be aware that you need to call ->close() at the end to write the zip file to disk.
You can easily chain most functions, except getFileContent, getStatus, close and extractTo which must come at the end of the chain.
The main reason I wrote this little package is the extractTo method since it allows you to be very flexible when extracting zips. So you can for example implement an update method which will just override the changed files.
Functions
make($pathToFile)
Create or Open a zip archive; if the file does not exists it will create a new one.
It will return the Zipper instance so you can chain easily.
add($files/folder)
You can add an array of Files, or a Folder and all the files in that folder will then be added, so from the first example we could instead do something like $files = 'public/files/';.
addString($filename, $content)
add a single file to the zip by specifying a name and the content as strings.
remove($file/s)
removes a single file or an array of files from the zip.
folder($folder)
Specify a folder to 'add files to' or 'remove files from' from the zip, example
Madzipper::make('test.zip')->folder('test')->add('composer.json'); Madzipper::make('test.zip')->folder('test')->remove('composer.json');
listFiles($regexFilter = null)
Lists all files within archive (if no filter pattern is provided). Use $regexFilter parameter to filter files. See Pattern Syntax for regular expression syntax
NB:
listFilesignores folder set withfolderfunction
Example: Return all files/folders ending/not ending with '.log' pattern (case insensitive). This will return matches in sub folders and their sub folders also
$logFiles = Madzipper::make('test.zip')->listFiles('/\.log$/i'); $notLogFiles = Madzipper::make('test.zip')->listFiles('/^(?!.*\.log).*$/i');
home()
Resets the folder pointer.
zip($fileName)
Uses the ZipRepository for file handling.
getFileContent($filePath)
get the content of a file in the zip. This will return the content or false.
getStatus()
get the opening status of the zip as integer.
close()
closes the zip and writes all changes.
extractTo($path)
Extracts the content of the zip archive to the specified location, for example
Madzipper::make('test.zip')->folder('test')->extractTo('foo');
This will go into the folder test in the zip file and extract the content of that folder only to the folder foo, this is equal to using the Madzipper::WHITELIST.
This command is really nice to get just a part of the zip file, you can also pass a 2nd & 3rd param to specify a single or an array of files that will be
NB: Php ZipArchive uses internally '/' as directory separator for files/folders in zip. So Windows users should not set whitelist/blacklist patterns with '' as it will not match anything
white listed
Madzipper::WHITELIST
Madzipper::make('test.zip')->extractTo('public', array('vendor'), Madzipper::WHITELIST);
Which will extract the test.zip into the public folder but only files/folders starting with vendor prefix inside the zip will be extracted.
or black listed
Madzipper::BLACKLIST Which will extract the
test.zipinto thepublicfolder except files/folders starting withvendorprefix inside the zip will not be extracted.
Madzipper::make('test.zip')->extractTo('public', array('vendor'), Madzipper::BLACKLIST);
Madzipper::EXACT_MATCH
Madzipper::make('test.zip') ->folder('vendor') ->extractTo('public', array('composer', 'bin/phpunit'), Madzipper::WHITELIST | Madzipper::EXACT_MATCH);
Which will extract the test.zip into the public folder but only files/folders exact matching names. So this will:
- extract file or folder named
composerin folder namedvendorinside zip topublicresultingpublic/composer - extract file or folder named
bin/phpunitinvendor/bin/phpunitfolder inside zip topublicresultingpublic/bin/phpunit
NB: extracting files/folder from zip without setting Madzipper::EXACT_MATCH When zip has similar structure as below and only
test.batis given as whitelist/blacklist argument thenextractTowould extract all those files and folders as they all start with given string
test.zip
|- test.bat
|- test.bat.~
|- test.bat.dir/
|- fileInSubFolder.log
extractMatchingRegex($path, $regex)
Extracts the content of the zip archive matching regular expression to the specified location. See Pattern Syntax for regular expression syntax.
Example: extract all files ending with .php from src folder and its sub folders.
Madzipper::make('test.zip')->folder('src')->extractMatchingRegex($path, '/\.php$/i');
Example: extract all files except those ending with test.php from src folder and its sub folders.
Madzipper::make('test.zip')->folder('src')->extractMatchingRegex($path, '/^(?!.*test\.php).*$/i');
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Development
Maybe it is a good idea to add other compression functions like rar, phar or bzip2 etc... Everything is setup for that, if you want just fork and develop further.
If you need other functions or got errors, please leave an issue on github.
Credits
Big thank you goes to @Chumper for creating the original package Chumper/Zipper
madnest/madzipper 适用场景与选型建议
madnest/madzipper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.47M 次下载、GitHub Stars 达 138, 最近一次更新时间为 2019 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「laravel」 「ziparchive」 「zip」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 madnest/madzipper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 madnest/madzipper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 madnest/madzipper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
a simple and powerful zip manipulation library
ZipArchive extended with support for UTF-8 encoded strings using base64.
Fork of nelexa/zip — PhpZip library for extended work with ZIP-archives. This fork adds PHP 8.5 support.
Factory class that creates either ZipArchive or PclZip PHP 5.2.17 and up
Clone a Github repository with PHP & ZipArchive. Without using Git or exec()!
PhpZip is a php-library for extended work with ZIP-archives. Open, create, update, delete, extract and get info tool. Supports appending to existing ZIP files, WinZip AES encryption, Traditional PKWARE Encryption, ZipAlign tool, BZIP2 compression, external file attributes and ZIP64 extensions. Alter
统计信息
- 总下载量: 2.47M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 141
- 点击次数: 28
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-05