originphp/zip
Composer 安装命令:
composer require originphp/zip
包简介
OriginPHP Zip
README 文档
README
A ZIP utility for creating and unziping ZIP files.
Installation
To install this package
$ composer require originphp/zip
Static Methods
The static methods provide a quick way to zip and unzip archives.
Create a ZIP Archive
To create a ZIP archive using a file or directory
Zip::zip(__DIR__ .'/src','/backups/today.zip');
You can also ZIP multiple files or directories
Zip::zip([ 'README.md', __DIR__ .'/src' ],'/backups/today.zip');
You can also pass any of the following options keys
- password: a password used to encrypt the archive with
- compress: default:
true. Set tofalseto only store the files (without compression) - encryption: default:
aes256. This is the encryption method used when using a password. Supported encryption methods:aes128,aes192,andaes256.
To encrypt files with passwords you need to be using PHP 7.3 or above.
Zip::zip(__DIR__ .'/src','/backups/today.zip',[ 'password'=>'passw0rd' ]);
Unzip a ZIP Archive
To unzip a ZIP file
Zip::unzip('/backups/today.zip','/a/folder');
If any of the files are encrypted you provide a password like this
Zip::unzip('/backups/today.zip','/a/folder',[ 'password' => 'passw0rd' ]);
Fluent Interface
The ZIP class also provides a fluent interface for working with ZIP archives.
Create a ZIP Archive
To create a new ZIP and files and directories. When you add a directory it will add all files and sub directories recursively.
$zip = new Zip(); $zip->create('/path/to/file.zip') ->add('README.md') ->add('src') ->save();
Encryption
To encrypt files with passwords you need to be using PHP 7.3 or above.
To encrypt all the files in the archive, call the encrypt method after adding the files, you can optionally supply the encryption method. Supported encryption methods are aes128,aes192 and aes256.
$zip = new Zip(); $zip->create('/path/to/file.zip') ->add('README.md') ->add('src') ->encrypt('passw0rd') ->save();
If just want to encrypt certain files
$zip = new Zip(); $zip->create('/path/to/file.zip') ->add('README.md') ->add('Financials.xlsx',['password' => 'secret']) ->save();
Compression
If you just want to store a file in the ZIP archive without compression you can set compress to false.
$zip = new Zip(); $zip->create('/path/to/file.zip') ->add('logo.jpg',['compress'=>false]) ->save();
Unzip a ZIP Archive
To extract files from a ZIP file
$zip = new Zip(); $zip->open('/path/to/file.zip') ->extract('/destination/folder')
If the ZIP file has encrypted files
$zip = new Zip(); $zip->open('/path/to/file.zip') ->extract('/destination/folder',['password'=>'foo']);
You can also extract selected files
$zip = new Zip(); $zip->open('/path/to/file.zip') ->extract('/destination/folder',[ 'files'=>[ 'README.md', 'LICENSE.md' ] ]);
Listing contents
To list contents of a ZIP file
$zip = new Zip(); $list = $zip->open('/path/to/file.zip') ->list();
This will output like this
Array
(
[0] => Origin\Zip\FileObject Object
(
[name] => README.md
[size] => 666
[timestamp] => 1576656596
[compressedSize] => 371
[encrypted] => 1
)
)
You can also just list files from within a folder of the ZIP file
$zip = new Zip(); $testFiles = $zip->open('/path/to/file.zip') ->list('tests');
Getting contents of a file
To get the contents of a file
$zip = new Zip(); $contents = $zip->open('/path/to/file.zip') ->get('README.md');
Renaming
To rename a file in a ZIP archive
$zip = new Zip(); $zip->open('/path/to/file.zip') ->rename('foo.txt','bar.txt');
Deleting Files
To delete a file from a ZIP archive
$zip = new Zip(); $zip->open('/path/to/file.zip') ->delete('tests/ControllerTest.php');
originphp/zip 适用场景与选型建议
originphp/zip 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 765 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 12 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「compression」 「zip」 「archive」 「originPHP」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 originphp/zip 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 originphp/zip 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 originphp/zip 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Asset Management for PHP
Image optimization / compression library. This library is able to optimize png, jpg and gif files in very easy and handy way. It uses optipng, pngquant, pngcrush, pngout, gifsicle, jpegoptim and jpegtran tools.
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.
Yii2 integration for TinyPng
统计信息
- 总下载量: 765
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-20