PhpZip是一个用于扩展ZIP存档工作的php库。打开、创建、更新、删除、提取和获取信息工具。支持向现有ZIP文件追加、WinZip AES加密、传统PKWARE加密、BZIP2压缩、外部文件属性和ZIP64扩展。替代ZipArchi

4.0.0 2022-05-16 01:30 UTC

README

PhpZip是一个用于扩展ZIP存档工作的php库。

Packagist Version Packagist Downloads Code Coverage Build Status License <<<<<<< HEAD

=======

74ef59704efd11eb19fe192d1a16772839230f2a

版本和依赖

版本和依赖

目录

特性

  • 打开和解压缩zip文件。
  • 创建ZIP存档。
  • 修改ZIP存档。
  • 纯php(不要求扩展php-zip和类\ZipArchive)。
  • 它支持将存档保存到文件、输出到浏览器,或者作为字符串输出而不保存到文件。
  • 支持存档注释和单个条目的注释。
  • 获取存档中每个条目的信息。
  • 仅支持以下压缩方法
    • 不压缩(存储)。
    • Deflate压缩。
    • 带有扩展php-bz2的BZIP2压缩。
  • 支持ZIP64(文件大小超过4GB或存档中的条目数超过65535)。
  • 处理密码

    注意!

    对于32位系统,目前不支持传统PKWARE加密(ZipCrypto)加密方法。尽可能使用加密方法WinZIP AES加密

    • 为所有条目或仅某些条目设置读取存档的密码。
    • 更改存档的密码,包括单个条目的密码。
    • 删除所有或单个条目的存档密码。
    • 为所有和存档中的单个条目设置密码和/或加密方法。
    • 为不同的条目设置不同的密码和加密方法。
    • 删除所有或某些条目的密码。
    • 支持 传统PKWARE加密(ZipCrypto)WinZIP AES加密 加密方法。
    • 设置存档中所有或单个条目的加密方法。

要求

  • PHP >= 7.4 或 PHP >= 8.0(推荐64位)。
  • 可选的php扩展 bzip2 用于BZIP2压缩。
  • 可选的php扩展 openssl 用于 WinZip Aes加密 支持。

安装

<<<<<<< HEAD composer require lyhiving/zip

composer require nelexa/zip

74ef59704efd11eb19fe192d1a16772839230f2a

最新稳定版本:最新稳定版本

示例

// create new archive
$zipFile = new \PhpZip\ZipFile();
try{
    $zipFile
        ->addFromString('zip/entry/filename', 'Is file content') // add an entry from the string
        ->addFile('/path/to/file', 'data/tofile') // add an entry from the file
        ->addDir(__DIR__, 'to/path/') // add files from the directory
        ->saveAsFile($outputFilename) // save the archive to a file
        ->close(); // close archive
            
    // open archive, extract, add files, set password and output to browser.
    $zipFile
        ->openFile($outputFilename) // open archive from file
        ->extractTo($outputDirExtract) // extract files to the specified directory
        ->deleteFromRegex('~^\.~') // delete all hidden (Unix) files
        ->addFromString('dir/file.txt', 'Test file') // add a new entry from the string
        ->setPassword('password') // set password for all entries
        ->outputAsAttachment('library.jar'); // output to the browser without saving to a file
}
catch(\PhpZip\Exception\ZipException $e){
    // handle exception
}
finally{
    $zipFile->close();
}

其他示例可以在 tests/ 文件夹中找到

术语表

ZIP条目 - ZIP存档中的文件或文件夹。存档中的每个条目都有某些属性,例如:文件名、压缩方法、加密方法、压缩前文件大小、压缩后文件大小、CRC32等。

文档

\PhpZip\ZipFile的方法概述

创建/打开ZIP存档

ZipFile::__construct**

初始化 ZIP 存档

$zipFile = new \PhpZip\ZipFile();
ZipFile::openFile

从文件打开 zip 存档。

$zipFile = new \PhpZip\ZipFile();
$zipFile->openFile('file.zip');
ZipFile::openFromString

从字符串打开 zip 存档。

$zipFile = new \PhpZip\ZipFile();
$zipFile->openFromString($stringContents);
ZipFile::openFromStream

从流打开 zip 存档。

$stream = fopen('file.zip', 'rb');

$zipFile = new \PhpZip\ZipFile();
$zipFile->openFromStream($stream);

从存档中读取条目

ZipFile::count

返回存档中的条目数量。

$zipFile = new \PhpZip\ZipFile();

$count = count($zipFile);
// or
$count = $zipFile->count();
ZipFile::getListFiles

返回存档文件列表。

$zipFile = new \PhpZip\ZipFile();
$listFiles = $zipFile->getListFiles();

// example array contents:
// array (
//   0 => 'info.txt',
//   1 => 'path/to/file.jpg',
//   2 => 'another path/',
//   3 => '0',
// )
ZipFile::getEntryContent

使用名称返回条目内容。

// $entryName = 'path/to/example-entry-name.txt';
$zipFile = new \PhpZip\ZipFile();

$contents = $zipFile[$entryName];
// or
$contents = $zipFile->getEntryContents($entryName);
ZipFile::hasEntry

检查存档中是否存在条目。

// $entryName = 'path/to/example-entry-name.txt';
$zipFile = new \PhpZip\ZipFile();

$hasEntry = isset($zipFile[$entryName]);
// or
$hasEntry = $zipFile->hasEntry($entryName);
ZipFile::isDirectory

检查存档中的条目是否为目录。

// $entryName = 'path/to/';
$zipFile = new \PhpZip\ZipFile();

$isDirectory = $zipFile->isDirectory($entryName);
ZipFile::extractTo

提取存档内容。目录必须存在。

$zipFile = new \PhpZip\ZipFile();
$zipFile->extractTo($directory);

将一些文件提取到目录中。目录必须存在。

// $toDirectory = '/tmp';
$extractOnlyFiles = [
    'filename1', 
    'filename2', 
    'dir/dir/dir/'
];
$zipFile = new \PhpZip\ZipFile();
$zipFile->extractTo($toDirectory, $extractOnlyFiles);

迭代条目

ZipFile 是一个迭代器。可以在 foreach 循环中遍历所有条目。

foreach($zipFile as $entryName => $contents){
    echo "Filename: $entryName" . PHP_EOL;
    echo "Contents: $contents" . PHP_EOL;
    echo '-----------------------------' . PHP_EOL;
}

可以遍历 Iterator

$iterator = new \ArrayIterator($zipFile);
while ($iterator->valid())
{
    $entryName = $iterator->key();
    $contents = $iterator->current();

    echo "Filename: $entryName" . PHP_EOL;
    echo "Contents: $contents" . PHP_EOL;
    echo '-----------------------------' . PHP_EOL;

    $iterator->next();
}

获取条目的信息

ZipFile::getArchiveComment

返回 Zip 存档注释。

$zipFile = new \PhpZip\ZipFile();
$commentArchive = $zipFile->getArchiveComment();
ZipFile::getEntryComment

使用条目名称返回条目注释。

$zipFile = new \PhpZip\ZipFile();
$commentEntry = $zipFile->getEntryComment($entryName);

将条目添加到存档

所有向 ZIP 存档添加条目的方法都允许您指定压缩内容的方法。

以下压缩方法可用

  • \PhpZip\Constants\ZipCompressionMethod::STORED - 无压缩
  • \PhpZip\Constants\ZipCompressionMethod::DEFLATED - Deflate 压缩
  • \PhpZip\Constants\ZipCompressionMethod::BZIP2 - 使用扩展 ext-bz2 的 Bzip2 压缩
ZipFile::addFile

从给定路径将文件添加到 ZIP 存档。

$zipFile = new \PhpZip\ZipFile();
// $file = '...../file.ext'; 
// $entryName = 'file2.ext'
$zipFile->addFile($file);

// you can specify the name of the entry in the archive (if null, then the last component from the file name is used)
$zipFile->addFile($file, $entryName);

// you can specify a compression method
$zipFile->addFile($file, $entryName, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
$zipFile->addFile($file, $entryName, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
$zipFile->addFile($file, $entryName, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
ZipFile::addSplFile

\SplFileInfo 添加到 ZIP 存档。

// $file = '...../file.ext'; 
// $entryName = 'file2.ext'
$zipFile = new \PhpZip\ZipFile();

$splFile = new \SplFileInfo('README.md');

$zipFile->addSplFile($splFile);
$zipFile->addSplFile($splFile, $entryName);
// or
$zipFile[$entryName] = new \SplFileInfo($file);

// set compression method
$zipFile->addSplFile($splFile, $entryName, $options = [
    \PhpZip\Constants\ZipOptions::COMPRESSION_METHOD => \PhpZip\Constants\ZipCompressionMethod::DEFLATED,
]);
ZipFile::addFromFinder

Symfony\Component\Finder\Finder 中的文件添加到 ZIP 存档。

$finder = new \Symfony\Component\Finder\Finder();
$finder
    ->files()
    ->name('*.{jpg,jpeg,gif,png}')
    ->name('/^[0-9a-f]\./')
    ->contains('/lorem\s+ipsum$/i')
    ->in('path');

$zipFile = new \PhpZip\ZipFile();
$zipFile->addFromFinder($finder, $options = [
    \PhpZip\Constants\ZipOptions::COMPRESSION_METHOD => \PhpZip\Constants\ZipCompressionMethod::DEFLATED,
    \PhpZip\Constants\ZipOptions::MODIFIED_TIME => new \DateTimeImmutable('-1 day 5 min')
]);
ZipFile::addFromString

使用内容将文件添加到 ZIP 存档。

$zipFile = new \PhpZip\ZipFile();

$zipFile[$entryName] = $contents;
// or
$zipFile->addFromString($entryName, $contents);

// you can specify a compression method
$zipFile->addFromString($entryName, $contents, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
$zipFile->addFromString($entryName, $contents, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
$zipFile->addFromString($entryName, $contents, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
ZipFile::addFromStream

将流中的条目添加到 ZIP 存档。

$zipFile = new \PhpZip\ZipFile();
// $stream = fopen(..., 'rb');

$zipFile->addFromStream($stream, $entryName);
// or
$zipFile[$entryName] = $stream;

// you can specify a compression method
$zipFile->addFromStream($stream, $entryName, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
$zipFile->addFromStream($stream, $entryName, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
$zipFile->addFromStream($stream, $entryName, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
ZipFile::addEmptyDir

添加新目录。

$zipFile = new \PhpZip\ZipFile();
// $path = "path/to/";
$zipFile->addEmptyDir($path);
// or
$zipFile[$path] = null;
ZipFile::addAll

从数组添加所有条目。

$entries = [
    'file.txt' => 'file contents', // add an entry from the string contents
    'empty dir/' => null, // add empty directory
    'path/to/file.jpg' => fopen('..../filename', 'rb'), // add an entry from the stream
    'path/to/file.dat' => new \SplFileInfo('..../filename'), // add an entry from the file
];

$zipFile = new \PhpZip\ZipFile();
$zipFile->addAll($entries);
ZipFile::addDir

从指定路径上的目录添加文件到存档,不包括子目录。

$zipFile = new \PhpZip\ZipFile();
$zipFile->addDir($dirName);

// you can specify the path in the archive to which you want to put entries
$localPath = 'to/path/';
$zipFile->addDir($dirName, $localPath);

// you can specify a compression method
$zipFile->addDir($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
$zipFile->addDir($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
$zipFile->addDir($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
ZipFile::addDirRecursive

从指定路径上的目录添加文件到存档,包括子目录。

$zipFile = new \PhpZip\ZipFile();
$zipFile->addDirRecursive($dirName);

// you can specify the path in the archive to which you want to put entries
$localPath = 'to/path/';
$zipFile->addDirRecursive($dirName, $localPath);

// you can specify a compression method
$zipFile->addDirRecursive($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
$zipFile->addDirRecursive($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
$zipFile->addDirRecursive($dirName, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
ZipFile::addFilesFromIterator

从目录迭代器添加文件。

// $directoryIterator = new \DirectoryIterator($dir); // without subdirectories
// $directoryIterator = new \RecursiveDirectoryIterator($dir); // with subdirectories
$zipFile = new \PhpZip\ZipFile();
$zipFile->addFilesFromIterator($directoryIterator);

// you can specify the path in the archive to which you want to put entries
$localPath = 'to/path/';
$zipFile->addFilesFromIterator($directoryIterator, $localPath);
// or
$zipFile[$localPath] = $directoryIterator;

// you can specify a compression method
$zipFile->addFilesFromIterator($directoryIterator, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
$zipFile->addFilesFromIterator($directoryIterator, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
$zipFile->addFilesFromIterator($directoryIterator, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression

一些文件忽略的示例

$ignoreFiles = [
    'file_ignore.txt', 
    'dir_ignore/sub dir ignore/'
];

// $directoryIterator = new \DirectoryIterator($dir); // without subdirectories
// $directoryIterator = new \RecursiveDirectoryIterator($dir); // with subdirectories
// use \PhpZip\Util\Iterator\IgnoreFilesFilterIterator for non-recursive search
 
$zipFile = new \PhpZip\ZipFile();
$ignoreIterator = new \PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator(
    $directoryIterator, 
    $ignoreFiles
);

$zipFile->addFilesFromIterator($ignoreIterator);
ZipFile::addFilesFromGlob

通过 glob 模式 从目录添加文件,不包括子目录。

$globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files

$zipFile = new \PhpZip\ZipFile();
$zipFile->addFilesFromGlob($dir, $globPattern);

// you can specify the path in the archive to which you want to put entries
$localPath = 'to/path/';
$zipFile->addFilesFromGlob($dir, $globPattern, $localPath);

// you can specify a compression method
$zipFile->addFilesFromGlob($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
$zipFile->addFilesFromGlob($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
$zipFile->addFilesFromGlob($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
ZipFile::addFilesFromGlobRecursive

通过 glob 模式 从目录添加文件,包括子目录。

$globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> add all .jpg, .jpeg, .png and .gif files

$zipFile = new \PhpZip\ZipFile();
$zipFile->addFilesFromGlobRecursive($dir, $globPattern);

// you can specify the path in the archive to which you want to put entries
$localPath = 'to/path/';
$zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath);

// you can specify a compression method
$zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
$zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
$zipFile->addFilesFromGlobRecursive($dir, $globPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
ZipFile::addFilesFromRegex

通过 PCRE 模式 从目录添加文件,不包括子目录。

$regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files

$zipFile = new \PhpZip\ZipFile();
$zipFile->addFilesFromRegex($dir, $regexPattern);

// you can specify the path in the archive to which you want to put entries
$localPath = 'to/path/';
$zipFile->addFilesFromRegex($dir, $regexPattern, $localPath);

// you can specify a compression method
$zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
$zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
$zipFile->addFilesFromRegex($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression
ZipFile::addFilesFromRegexRecursive

通过 PCRE 模式 从目录添加文件,包括子目录。

$regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> add all .jpg, .jpeg, .png and .gif files

$zipFile->addFilesFromRegexRecursive($dir, $regexPattern);

// you can specify the path in the archive to which you want to put entries
$localPath = 'to/path/';
$zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath);

// you can specify a compression method
$zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::STORED); // No compression
$zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::DEFLATED); // Deflate compression
$zipFile->addFilesFromRegexRecursive($dir, $regexPattern, $localPath, \PhpZip\Constants\ZipCompressionMethod::BZIP2); // BZIP2 compression

从存档中删除条目

ZipFile::deleteFromName

使用名称从存档中删除条目。

$zipFile = new \PhpZip\ZipFile();
$zipFile->deleteFromName($entryName);
ZipFile::deleteFromGlob

使用 glob 模式 从存档中删除条目。

$globPattern = '**.{jpg,jpeg,png,gif}'; // example glob pattern -> delete all .jpg, .jpeg, .png and .gif files

$zipFile = new \PhpZip\ZipFile();
$zipFile->deleteFromGlob($globPattern);
ZipFile::deleteFromRegex

使用PCRE模式删除存档中的条目。

$regexPattern = '/\.(jpe?g|png|gif)$/si'; // example regex pattern -> delete all .jpg, .jpeg, .png and .gif files

$zipFile = new \PhpZip\ZipFile();
$zipFile->deleteFromRegex($regexPattern);
ZipFile::deleteAll

删除ZIP存档中的所有条目。

$zipFile = new \PhpZip\ZipFile();
$zipFile->deleteAll();

处理条目和存档

ZipFile::rename

按名称重命名条目。

$zipFile = new \PhpZip\ZipFile();
$zipFile->rename($oldName, $newName);
ZipFile::setCompressionLevel

设置存档中所有文件的压缩级别。

请注意,此方法不适用于在此方法运行之后添加的条目。

默认情况下,压缩级别为5(\PhpZip\Constants\ZipCompressionLevel::NORMAL)或存档中指定的Deflate压缩级别。

支持的值范围从1(\PhpZip\Constants\ZipCompressionLevel::SUPER_FAST)到9(\PhpZip\Constants\ZipCompressionLevel::MAXIMUM)。数值越高,压缩效果越好,所需时间越长。

$zipFile = new \PhpZip\ZipFile();
$zipFile->setCompressionLevel(\PhpZip\Constants\ZipCompressionLevel::MAXIMUM);
ZipFile::setCompressionLevelEntry

通过名称设置条目的压缩级别。

支持的值范围从1(\PhpZip\Constants\ZipCompressionLevel::SUPER_FAST)到9(\PhpZip\Constants\ZipCompressionLevel::MAXIMUM)。数值越高,压缩效果越好,所需时间越长。

$zipFile = new \PhpZip\ZipFile();
$zipFile->setCompressionLevelEntry($entryName, \PhpZip\Constants\ZipCompressionLevel::FAST);
ZipFile::setCompressionMethodEntry

通过名称设置条目的压缩方法。

以下是一些可用的压缩方法

  • \PhpZip\Constants\ZipCompressionMethod::STORED - 不压缩
  • \PhpZip\Constants\ZipCompressionMethod::DEFLATED - Deflate 压缩
  • \PhpZip\Constants\ZipCompressionMethod::BZIP2 - 使用扩展 ext-bz2 的 Bzip2 压缩
$zipFile = new \PhpZip\ZipFile();
$zipFile->setCompressionMethodEntry($entryName, \PhpZip\Constants\ZipCompressionMethod::DEFLATED);
ZipFile::setArchiveComment

设置ZIP存档的注释。

$zipFile = new \PhpZip\ZipFile();
$zipFile->setArchiveComment($commentArchive);
ZipFile::setEntryComment

设置按名称定义的条目的注释。

$zipFile = new \PhpZip\ZipFile();
$zipFile->setEntryComment($entryName, $comment);
ZipFile::matcher

选择存档中的条目以执行操作。

$zipFile = new \PhpZip\ZipFile();
$matcher = $zipFile->matcher();

一次选择存档中的一个文件

$matcher
    ->add('entry name')
    ->add('another entry');

选择存档中的多个文件

$matcher->add([
    'entry name',
    'another entry name',
    'path/'
]);

按正则表达式选择文件

$matcher->match('~\.jpe?g$~i');

选择存档中的所有文件

$matcher->all();

count() - 获取所选条目的数量

$count = count($matcher);
// or
$count = $matcher->count();

getMatches() - 返回所选条目的列表

$entries = $matcher->getMatches();
// example array contents: ['entry name', 'another entry name'];

invoke() - 在所选条目上调用可调用的函数

// example
$matcher->invoke(static function($entryName) use($zipFile) {
    $newName = preg_replace('~\.(jpe?g)$~i', '.no_optimize.$1', $entryName);
    $zipFile->rename($entryName, $newName);
});

处理所选条目的函数

$matcher->delete(); // remove selected entries from a ZIP archive
$matcher->setPassword($password); // sets a new password for the selected entries
$matcher->setPassword($password, $encryptionMethod); // sets a new password and encryption method to selected entries
$matcher->setEncryptionMethod($encryptionMethod); // sets the encryption method to the selected entries
$matcher->disableEncryption(); // disables encryption for selected entries

处理密码

实现了加密方法的支持

  • \PhpZip\Constants\ZipEncryptionMethod::PKWARE - 传统PKWARE加密(已弃用)
  • \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_256 - WinZip AES加密 256位(推荐)
  • \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_192 - WinZip AES加密 192位
  • \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_128 - WinZip AES加密 128位
ZipFile::setReadPassword

设置打开存档的密码。

添加新条目或删除现有条目时不需要设置密码,但如果您想提取内容或更改方法/压缩级别、加密方法或更改密码,在这种情况下必须指定密码。

$zipFile->setReadPassword($password);
ZipFile::setReadPasswordEntry

为按名称定义的条目的读取获取密码。

$zipFile->setReadPasswordEntry($entryName, $password);
ZipFile::setPassword

为存档中的所有文件设置新密码。

请注意,此方法不适用于在此方法运行之后添加的条目。

$zipFile->setPassword($password);

您可以设置加密方法

$encryptionMethod = \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_256;
$zipFile->setPassword($password, $encryptionMethod);
ZipFile::setPasswordEntry

为按名称定义的条目设置新密码。

$zipFile->setPasswordEntry($entryName, $password);

您可以设置加密方法

$encryptionMethod = \PhpZip\Constants\ZipEncryptionMethod::WINZIP_AES_256;
$zipFile->setPasswordEntry($entryName, $password, $encryptionMethod);
ZipFile::disableEncryption

禁用存档中所有条目的加密。

请注意,此方法不适用于在此方法运行之后添加的条目。

$zipFile->disableEncryption();
ZipFile::disableEncryptionEntry

禁用按名称定义的条目的加密。

$zipFile->disableEncryptionEntry($entryName);

撤销更改

ZipFile::unchangeAll

撤销存档中进行的所有更改。

$zipFile->unchangeAll();
ZipFile::unchangeArchiveComment

撤销存档注释的更改。

$zipFile->unchangeArchiveComment();
ZipFile::unchangeEntry

撤销按名称定义的条目的更改。

$zipFile->unchangeEntry($entryName);

保存文件或输出到浏览器

ZipFile::saveAsFile

将存档保存到文件。

$zipFile->saveAsFile($filename);
ZipFile::saveAsStream

将存档写入流。

// $fp = fopen($filename, 'w+b');

$zipFile->saveAsStream($fp);
ZipFile::outputAsString

将ZIP存档作为字符串输出。

$rawZipArchiveBytes = $zipFile->outputAsString();
ZipFile::outputAsAttachment

将ZIP存档输出到浏览器。

$zipFile->outputAsAttachment($outputFilename);

您可以设置Mime-Type

$mimeType = 'application/zip';
$zipFile->outputAsAttachment($outputFilename, $mimeType);
ZipFile::outputAsPsr7Response

将ZIP存档作为PSR-7响应输出。

输出方法可以在任何PSR-7兼容的框架中使用。

// $response = ....; // instance Psr\Http\Message\ResponseInterface
$zipFile->outputAsPsr7Response($response, $outputFilename);

您可以设置Mime-Type

$mimeType = 'application/zip';
$zipFile->outputAsPsr7Response($response, $outputFilename, $mimeType);
ZipFile::outputAsSymfonyResponse

将ZIP存档作为Symfony响应输出。

输出方法可以在Symfony框架中使用。

$response = $zipFile->outputAsSymfonyResponse($outputFilename);

您可以设置Mime-Type

$mimeType = 'application/zip';
$response = $zipFile->outputAsSymfonyResponse($outputFilename, $mimeType);

在Symfony控制器中的示例使用

<?php

namespace App\Controller;

use PhpZip\ZipFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DownloadZipController
{
    /**
     * @Route("/downloads/{id}")
     *
     * @throws \PhpZip\Exception\ZipException
     */
    public function __invoke(string $id): Response
    {
        $zipFile = new ZipFile();
        $zipFile['file'] = 'contents';

        $outputFilename = $id . '.zip';
        return $zipFile->outputAsSymfonyResponse($outputFilename);
    }
}
ZipFile::rewrite

保存更改并重新打开更改后的存档。

$zipFile->rewrite();

关闭存档

ZipFile::close

关闭存档。

$zipFile->close();

运行测试

安装开发依赖项

composer install --dev

运行测试

vendor/bin/phpunit

变更日志

更改已记录在发布页面

升级

将版本3升级到版本4

将文件composer.json中的主版本更新到^4.0

{
    "require": {
        "nelexa/zip": "^4.0"
    }
}

然后使用Composer安装更新

composer update nelexa/zip

更新您的代码以与新版本兼容:BC

  • 移除了已过时的类和方法。
  • 移除了zipalign功能。此功能将被放置在单独的包nelexa/apkfile中。

将版本2升级到版本3

将文件composer.json中的主版本更新到^3.0

{
    "require": {
        "nelexa/zip": "^3.0"
    }
}

然后使用Composer安装更新

composer update nelexa/zip

更新您的代码以与新版本兼容

  • 将类ZipOutputFile合并到ZipFile并移除。
    • new \PhpZip\ZipOutputFile()改为new \PhpZip\ZipFile()
  • 静态初始化方法现在不再是静态的。
    • \PhpZip\ZipFile::openFromFile($filename);改为(new \PhpZip\ZipFile())->openFile($filename);
    • \PhpZip\ZipOutputFile::openFromFile($filename);改为(new \PhpZip\ZipFile())->openFile($filename);
    • \PhpZip\ZipFile::openFromString($contents);改为(new \PhpZip\ZipFile())->openFromString($contents);
    • \PhpZip\ZipFile::openFromStream($stream);改为(new \PhpZip\ZipFile())->openFromStream($stream);
    • \PhpZip\ZipOutputFile::create()改为new \PhpZip\ZipFile()
    • \PhpZip\ZipOutputFile::openFromZipFile(\PhpZip\ZipFile $zipFile)改为(new \PhpZip\ZipFile())->openFile($filename);
  • 重命名方法
    • addFromFile改为addFile
    • setLevel改为setCompressionLevel
    • ZipFile::setPassword改为ZipFile::withReadPassword
    • ZipOutputFile::setPassword改为ZipFile::withNewPassword
    • ZipOutputFile::disableEncryptionAllEntries改为ZipFile::withoutPassword
    • ZipOutputFile::setComment改为ZipFile::setArchiveComment
    • ZipFile::getComment改为ZipFile::getArchiveComment
  • 修改了addDiraddFilesFromGlobaddFilesFromRegex方法的签名。
  • 移除方法
    • getLevel
    • setCompressionMethod
    • setEntryPassword