zanysoft/laravel-zip
Composer 安装命令:
composer require zanysoft/laravel-zip
包简介
laravel-zip is the world's leading zip utility for file compression and backup.
关键字:
README 文档
README
laravel-zip is the world's leading zip utility for file compression and backup.
This library was written to simplify and automate Zip files management using PHP ZipArchive.
Installation
Require this package in your composer.json and update composer. This will download the package.
composer require zanysoft/laravel-zip
After updating composer, add the ServiceProvider to the providers array in config/app.php
ZanySoft\Zip\ZipServiceProvider::class,
You can optionally use the facade for shorter code. Add this to your facades:
'Zip' => ZanySoft\Zip\Facades\Zip::class,
Zip usage
use ZanySoft\Zip\Zip class only use Zip is made to handle a zip file.
- include the Zip class at top:
use Zip;
Basic operations
-
Open zip file:
$zip = Zip::open('file.zip');
-
Create zip file:
$zip = Zip::create('file.zip');
-
Check zip file:
$is_valid = Zip::check('file.zip');
-
Extract zip file:
// extract whole archive $zip->extract('/path/to/uncompressed/files'); // extract a file $zip->extract('/path/to/uncompressed/files', 'file'); // extract multiple files $zip->extract('/path/to/uncompressed/files', array('file1','file2'));
-
Check if zip has a file:
// Check if archive has a file $zip->has('/path/to/file/in/archive'); // Check if archive has a file case insensitively $zip->has('/path/to/file/in/archive', ZipArchive::FL_NOCASE); // Check if archive has a file ignoring directory component $zip->has('file', ZipArchive::FL_NODIR); // Check if archive has a file case insensitively ignoring directory component $zip->has('file', ZipArchive::FL_NOCASE|ZipArchive::FL_NODIR);
-
Add a file/directory to zip:
$zip->add('/path/to/my/file'); // declaring path $zip->setPath('/path/to/my')->add('file'); // add directory $zip->add('/path/to/my/directory'); // add directory (only its content) $zip->add('/path/to/my/directory', true);
-
Add content to zip:
$zip->addFromString('file name with extension', 'content of file'); $zip->addFromString('filename.txt', $file_content); $zip->addFromString('folder/file1.txt', $file_content); $zip->addFromString('folder/file2.txt', $file_content);
-
Add multiple files/directories to zip:
// using array as parameter $zip->add( array('/path/to/my/file1', '/path/to/my/file2'); // chaining methods $zip->add('/path/to/my/file1')->add('/path/to/my/file2'); // declaring path $zip->setPath('/path/to/my')->add('file1')->add('file2');
-
Delete a file/directory from zip:
$zip->delete('file');
-
Delete multiple files/directories from zip:
// using array as parameter $zip->delete( array('file1', 'file2') ); // chaining methods $zip->delete('file1')->delete('file2');
-
List content of zip file
$zip->listFiles();
-
Close zip file
$zip->close();
Additional methods
-
Skip hidden files while adding directories:
// set mode $zip->setSkipped('HIDDEN'); // get mode $mode = $zip->getSkipped();
-
Use password for zip extraction:
// set password $zip->setPassword('slartibartfast'); // get password $password = $zip->getPassword();
-
Use a mask != 0777 for created folders:
// set mask $zip->setMask(0644); // get mask $mask = $zip->getMask();
ZipManager usage
The \ZanySoft\Zip\ZipManager can handle multiple ZanySoft\Zip\Zip objects.
- include the Zip and ZipManager class at top:
use ZanySoft\Zip\ZipManager; use Zip;
Basic operations
-
Init the manager and register Zips:
// init manager $manager = new ZipManager(); // register existing zips $manager->addZip( Zip::open('/path/to/my/file1.zip') ) ->addZip( Zip::open('/path/to/my/file2.zip') ); // register a new zip $manager->addZip( Zip::create('/path/to/my/file3.zip') );
-
Basic zips management:
// get a list of registered zips $list = $manager->listZips(); // remove a zip $manager->removeZip($ZipObject); // get a Zip $zip = $manager->getZip(0);
-
Add files to all zips:
$manager = new ZipManager(); // register existing zips $manager->addZip( Zip::open('/path/to/my/file1.zip') ) ->addZip( Zip::open('/path/to/my/file2.zip') ); // register a new zip $manager->addZip( Zip::create('/path/to/my/file3.zip') );
-
Extract zips:
// separate content in folders $extract = $manager->extract('/path/to/uncompressed/files', true); // use a single folder $extract = $manager->extract('/path/to/uncompressed/files', false); // extract single file $extract = $manager->extract('/path/to/uncompressed/files', false, 'file'); // extract multiple files $extract = $manager->extract('/path/to/uncompressed/files', false, array('file1','file2'));
-
Merge zips:
// separate content in folders $manager->merge('/path/to/output/file.zip', true); // flatten files $manager->merge('/path/to/output/file.zip', false);
-
Close zips:
$manager->close();
Additional methods
-
Declare path from which add files:
// set path $zip->setPath('/path/to/files'); // get path $path = $zip->getPath();
-
Use a mask != 0777 for created folders
// set masks $manager->setMask(0644); // get masks $mask = $manager->getMask();
zanysoft/laravel-zip 适用场景与选型建议
zanysoft/laravel-zip 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.97M 次下载、GitHub Stars 达 314, 最近一次更新时间为 2017 年 05 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「multiple」 「backup」 「laravel」 「extract」 「unzip」 「ziparchive」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zanysoft/laravel-zip 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zanysoft/laravel-zip 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zanysoft/laravel-zip 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A SDK for working with B2 cloud storage.
Extensions for laravel's database abstraction layer
A sleek PHP wrapper around rclone with Laravel-style fluent API syntax
Extension of Magento2 Sales Rule to allow multiple coupon usage.
A simple database backup manager for Symfony2 with support for S3, Rackspace, Dropbox, FTP, SFTP.
Fix failed Craft backups and restores with full paths to psql, mysql, and mysqldump executables.
统计信息
- 总下载量: 2.97M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 320
- 点击次数: 24
- 依赖项目数: 17
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-26