PhpZip是一個用於擴充 ZIP 檔案工作的 php 函式庫。
俄語文檔
| 版本 | PHP | 文件 |
|---|---|---|
| ^4.0(主) | ^7.4|^8.0 | 目前的 |
| ^3.0 | ^5.5|^7.0 | 文檔 v3.3 |
PhpZipZipFile類別的方法概述php-zip和類別ZipArchive )。php-bz2 。ZIP64 (檔案大小超過 4 GB 或存檔中的條目數超過 65535)。注意力!
對於 32 位元系統,目前不支援
Traditional PKWARE Encryption (ZipCrypto)加密方法。盡可能使用加密方法WinZIP AES Encryption。
Traditional PKWARE Encryption (ZipCrypto)和WinZIP AES Encryption加密方式。PHP >= 7.4 或PHP >= 8.0(最好是 64 位元)。bzip2 。WinZip Aes Encryption支援的可選 php 擴充openssl 。composer require nelexa/zip
最新穩定版本:
// 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 等。
PhpZipZipFile類別的方法概述SplFileInfo新增至 ZIP 檔案。SymfonyComponentFinderFinder中的檔案加入到 ZIP 檔案中。初始化 ZIP 存檔
$ zipFile = new PhpZip ZipFile ();從檔案開啟 zip 檔案。
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> openFile ( ' file.zip ' );從字串開啟 zip 檔案。
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> openFromString ( $ stringContents );從流中開啟 zip 檔案。
$ stream = fopen ( ' file.zip ' , ' rb ' );
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> openFromStream ( $ stream );傳回存檔中的條目數。
$ zipFile = new PhpZip ZipFile ();
$ count = count ( $ zipFile );
// or
$ count = $ zipFile -> count ();返回存檔文件列表。
$ zipFile = new PhpZip ZipFile ();
$ listFiles = $ zipFile -> getListFiles ();
// example array contents:
// array (
// 0 => 'info.txt',
// 1 => 'path/to/file.jpg',
// 2 => 'another path/',
// 3 => '0',
// ) 使用條目名稱傳回條目內容。
// $entryName = 'path/to/example-entry-name.txt';
$ zipFile = new PhpZip ZipFile ();
$ contents = $ zipFile [ $ entryName ];
// or
$ contents = $ zipFile -> getEntryContents ( $ entryName );檢查存檔中是否有條目。
// $entryName = 'path/to/example-entry-name.txt';
$ zipFile = new PhpZip ZipFile ();
$ hasEntry = isset ( $ zipFile [ $ entryName ]);
// or
$ hasEntry = $ zipFile -> hasEntry ( $ entryName );檢查存檔中的條目是否為目錄。
// $entryName = 'path/to/';
$ zipFile = new PhpZip ZipFile ();
$ isDirectory = $ zipFile -> isDirectory ( $ entryName );提取存檔內容。該目錄必須存在。
$ 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 ();
}返回 Zip 存檔註解。
$ zipFile = new PhpZip ZipFile ();
$ commentArchive = $ zipFile -> getArchiveComment ();使用條目名稱傳回條目的註釋。
$ zipFile = new PhpZip ZipFile ();
$ commentEntry = $ zipFile -> getEntryComment ( $ entryName );所有向 ZIP 檔案新增條目的方法都允許您指定壓縮內容的方法。
可以使用以下壓縮方法:
PhpZipConstantsZipCompressionMethod::STORED - 不壓縮PhpZipConstantsZipCompressionMethod::DEFLATED - 放氣壓縮PhpZipConstantsZipCompressionMethod::BZIP2 - 擴展名為ext-bz2的 Bzip2 壓縮將檔案從給定路徑新增至 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 將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 ,
]);將SymfonyComponentFinderFinder中的檔案加入 ZIP 檔案中。
$ finder = new Symfony Component Finder Finder ();
$ finder
-> files ()
-> name ( ' *.{jpg,jpeg,gif,png} ' )
-> name ( ' /^[0-9a-f]./ ' )
-> contains ( ' /lorems+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 ' )
]);使用檔案內容將檔案新增至 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 將流中的條目新增至 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 = new PhpZip ZipFile ();
// $path = "path/to/";
$ zipFile -> addEmptyDir ( $ path );
// or
$ zipFile [ $ path ] = null ;新增數組中的所有條目。
$ 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 = 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 = 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 從目錄迭代器新增檔案。
// $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 PhpZipUtilIteratorIgnoreFilesFilterIterator for non-recursive search
$ zipFile = new PhpZip ZipFile ();
$ ignoreIterator = new PhpZip Util Iterator IgnoreFilesRecursiveFilterIterator (
$ directoryIterator ,
$ ignoreFiles
);
$ zipFile -> addFilesFromIterator ( $ ignoreIterator );透過 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 透過帶有子目錄的 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 透過 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 透過帶有子目錄的 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 = new PhpZip ZipFile ();
$ zipFile -> deleteFromName ( $ entryName );使用 glob 模式刪除存檔中的項目。
$ globPattern = ' **.{jpg,jpeg,png,gif} ' ; // example glob pattern -> delete all .jpg, .jpeg, .png and .gif files
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> deleteFromGlob ( $ globPattern );使用 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 );刪除 ZIP 檔案中的所有條目。
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> deleteAll ();重新命名由其名稱定義的條目。
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> rename ( $ oldName , $ newName );設定存檔中所有檔案的壓縮等級。
請注意,此方法不適用於執行此方法後新增的條目。
預設情況下,壓縮等級為 5 ( PhpZipConstantsZipCompressionLevel::NORMAL ) 或檔案中為 Deflate 壓縮指定的壓縮等級。
支援的值範圍為 1 ( PhpZipConstantsZipCompressionLevel::SUPER_FAST ) 到 9 ( PhpZipConstantsZipCompressionLevel::MAXIMUM )。數字越大,壓縮效果越好、時間越長。
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> setCompressionLevel ( PhpZip Constants ZipCompressionLevel:: MAXIMUM );依條目名稱設定條目的壓縮等級。
支援的值範圍為 1 ( PhpZipConstantsZipCompressionLevel::SUPER_FAST ) 到 9 ( PhpZipConstantsZipCompressionLevel::MAXIMUM )。數字越大,壓縮效果越好、時間越長。
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> setCompressionLevelEntry ( $ entryName , PhpZip Constants ZipCompressionLevel:: FAST );依條目名稱設定條目的壓縮方法。
可以使用以下壓縮方法:
PhpZipConstantsZipCompressionMethod::STORED - 無壓縮PhpZipConstantsZipCompressionMethod::DEFLATED - 放氣壓縮PhpZipConstantsZipCompressionMethod::BZIP2 - 擴展名為ext-bz2的 Bzip2 壓縮 $ zipFile = new PhpZip ZipFile ();
$ zipFile -> setCompressionMethodEntry ( $ entryName , PhpZip Constants ZipCompressionMethod:: DEFLATED );設定 ZIP 存檔的註解。
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> setArchiveComment ( $ commentArchive );設定由名稱定義的條目的註釋。
$ zipFile = new PhpZip ZipFile ();
$ zipFile -> setEntryComment ( $ entryName , $ comment );選擇存檔中的條目以對其執行操作。
$ 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 實現了對加密方法的支援:
PhpZipConstantsZipEncryptionMethod::PKWARE - 傳統 PKWARE 加密(遺留)PhpZipConstantsZipEncryptionMethod::WINZIP_AES_256 - WinZip AES 加密 256 位元(建議)PhpZipConstantsZipEncryptionMethod::WINZIP_AES_192 - WinZip AES 加密 192 位PhpZipConstantsZipEncryptionMethod::WINZIP_AES_128 - WinZip AES 加密 128 位設定開啟檔案的密碼。
新增條目或刪除現有條目不需要設定密碼,但如果要提取內容或更改方法/壓縮等級、加密方法或更改密碼,在這種情況下必須指定密碼。
$ zipFile -> setReadPassword ( $ password );取得用於讀取由其名稱定義的條目的密碼。
$ zipFile -> setReadPasswordEntry ( $ entryName , $ password );為存檔中的所有檔案設定新密碼。
請注意,此方法不適用於執行此方法後新增的條目。
$ zipFile -> setPassword ( $ password );您可以設定加密方法:
$ encryptionMethod = PhpZip Constants ZipEncryptionMethod:: WINZIP_AES_256 ;
$ zipFile -> setPassword ( $ password , $ encryptionMethod );設定由其名稱定義的條目的新密碼。
$ zipFile -> setPasswordEntry ( $ entryName , $ password );您可以設定加密方法:
$ encryptionMethod = PhpZip Constants ZipEncryptionMethod:: WINZIP_AES_256 ;
$ zipFile -> setPasswordEntry ( $ entryName , $ password , $ encryptionMethod );對存檔中已有的所有條目停用加密。
請注意,此方法不適用於執行此方法後新增的條目。
$ zipFile -> disableEncryption ();禁用由其名稱定義的條目的加密。
$ zipFile -> disableEncryptionEntry ( $ entryName );撤銷存檔中所做的所有變更。
$ zipFile -> unchangeAll ();撤銷存檔註解的變更。
$ zipFile -> unchangeArchiveComment ();撤銷對由其名稱定義的條目的變更。
$ zipFile -> unchangeEntry ( $ entryName );將存檔儲存到文件中。
$ zipFile -> saveAsFile ( $ filename );將存檔寫入流。
// $fp = fopen($filename, 'w+b');
$ zipFile -> saveAsStream ( $ fp );將 ZIP 檔案輸出為字串。
$ rawZipArchiveBytes = $ zipFile -> outputAsString ();將 ZIP 檔案輸出到瀏覽器。
$ zipFile -> outputAsAttachment ( $ outputFilename );您可以設定 Mime-Type:
$ mimeType = ' application/zip ' ;
$ zipFile -> outputAsAttachment ( $ outputFilename , $ mimeType );輸出 ZIP 檔案作為 PSR-7 響應。
輸出方法可以在任何 PSR-7 相容框架中使用。
// $response = ....; // instance PsrHttpMessageResponseInterface
$ zipFile -> outputAsPsr7Response ( $ response , $ outputFilename );您可以設定 Mime-Type:
$ mimeType = ' application/zip ' ;
$ zipFile -> outputAsPsr7Response ( $ response , $ outputFilename , $ mimeType );將 ZIP 檔案輸出為 Symfony Response。
輸出方法可以在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 PhpZipExceptionZipException
*/
public function __invoke ( string $ id ): Response
{
$ zipFile = new ZipFile ();
$ zipFile [ ' file ' ] = ' contents ' ;
$ outputFilename = $ id . ' .zip ' ;
return $ zipFile -> outputAsSymfonyResponse ( $ outputFilename );
}
}儲存變更並重新開啟變更的檔案。
$ zipFile -> rewrite ();關閉存檔。
$ zipFile -> close ();安裝開發的依賴項:
composer install --dev運行測試:
vendor/bin/phpunit更改記錄在發布頁面中。
將文件composer.json中的主要版本更新為^4.0 。
{
"require" : {
"nelexa/zip" : " ^4.0 "
}
}然後使用Composer安裝更新:
composer update nelexa/zip更新您的程式碼以使用新版本: BC
zipalign功能。此功能將放置在單獨的套件nelexa/apkfile中。 將文件composer.json中的主要版本更新為^3.0 。
{
"require" : {
"nelexa/zip" : " ^3.0 "
}
}然後使用Composer安裝更新:
composer update nelexa/zip更新您的程式碼以使用新版本:
ZipOutputFile類別合併到ZipFile並刪除。new PhpZipZipOutputFile()到new PhpZipZipFile()PhpZipZipFile::openFromFile($filename);到(new PhpZipZipFile())->openFile($filename);PhpZipZipOutputFile::openFromFile($filename);到(new PhpZipZipFile())->openFile($filename);PhpZipZipFile::openFromString($contents);到(new PhpZipZipFile())->openFromString($contents);PhpZipZipFile::openFromStream($stream);到(new PhpZipZipFile())->openFromStream($stream);PhpZipZipOutputFile::create()到new PhpZipZipFile()PhpZipZipOutputFile::openFromZipFile(PhpZipZipFile $zipFile) > (new PhpZipZipFile())->openFile($filename);addFromFile到addFilesetLevel為setCompressionLevelZipFile::setPassword到ZipFile::withReadPasswordZipOutputFile::setPassword到ZipFile::withNewPasswordZipOutputFile::disableEncryptionAllEntries至ZipFile::withoutPasswordZipOutputFile::setComment到ZipFile::setArchiveCommentZipFile::getComment到ZipFile::getArchiveCommentaddDir 、 addFilesFromGlob 、 addFilesFromRegex的簽章。getLevelsetCompressionMethodsetEntryPassword