arnapou/zip

库 - Zip实用库。

v1.3 2024-04-14 15:59 UTC

This package is auto-updated.

Last update: 2024-09-08 15:05:50 UTC


README

pipeline coverage

此库提供了一些实用类来管理zip文件。

基本上,它

  • 通过抛出异常来移除ZipArchive的所有虚假行为
  • 为95%的使用场景提供安全的方法
  • 将"读取"和"写入"拆分为专用对象以降低进行奇怪操作的风险

安装

composer require arnapou/zip

packagist 👉️ arnapou/zip

读取器

目标是提供一种只读方式,使用专用对象读取/提取zip存档。

$reader = new \Arnapou\Zip\ZipReader($zipFilename);

foreach ($reader as $item) {
    // $item is :
    // - either a \Arnapou\Zip\Reading\Zipped\ZippedDir object
    // - either a \Arnapou\Zip\Reading\Zipped\ZippedFile object
}

// But you can iterate directly of on files. 
foreach ($reader->getFiles() as $zippedFile) {
    // Only files
}

// Or on Dirs. 
foreach ($reader->getDirs() as $zippedDir) {
    // Only dirs
}

写入器

此写入器包装器仅执行写入操作。

$writer = new \Arnapou\Zip\ZipWriter($zipFilename);

$writer->addFileContent('some-file.txt', 'Hello World');
$writer->addPath('/some/path/to/zip');
$writer->addFile('/some/file.txt', 'file.txt');

$writer->close();

对于大文件,您可以通过更改默认适配器来流式写入zip文件。

$writer = new \Arnapou\Zip\ZipWriter(
    $zipFilename,
    new \Arnapou\Zip\Writing\Adapter\ZipStreamWriteOnly()
);

$writer->addFile('/some/really/huge/file.txt', 'file.txt');

$writer->close();

PHP版本

日期参考8.3
15/01/20241.x, 主分支×