PhpZip ZIP アーカイブを使用した拡張作業のための php ライブラリです。
ロシア語のドキュメント
| バージョン | PHP | ドキュメント |
|---|---|---|
| ^4.0 (マスター) | ^7.4|^8.0 | 現在 |
| ^3.0 | ^5.5|^7.0 | ドキュメント v3.3 |
PhpZipZipFileのメソッドの概要php-zipとクラスZipArchiveは必要ありません)。php-bz2を持つ BZIP2 圧縮。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-extension 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 Entry - 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 ();アーカイブからファイルを一度に 1 つずつ選択します。
$ 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 タイプを設定できます。
$ mimeType = ' application/zip ' ;
$ zipFile -> outputAsAttachment ( $ outputFilename , $ mimeType );ZIP アーカイブを PSR-7 応答として出力します。
この出力方法は、PSR-7 互換のフレームワークで使用できます。
// $response = ....; // instance PsrHttpMessageResponseInterface
$ zipFile -> outputAsPsr7Response ( $ response , $ outputFilename );Mime タイプを設定できます。
$ mimeType = ' application/zip ' ;
$ zipFile -> outputAsPsr7Response ( $ response , $ outputFilename , $ mimeType );ZIP アーカイブを Symfony レスポンスとして出力します。
出力メソッドは Symfony フレームワークで使用できます。
$ response = $ zipFile -> outputAsSymfonyResponse ( $ outputFilename );Mime タイプを設定できます。
$ 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からaddFileへsetLevelからsetCompressionLevelZipFile::setPasswordからZipFile::withReadPasswordZipOutputFile::setPasswordからZipFile::withNewPasswordZipOutputFile::disableEncryptionAllEntriesからZipFile::withoutPasswordへZipOutputFile::setCommentからZipFile::setArchiveCommentZipFile::getCommentからZipFile::getArchiveCommentaddDir 、 addFilesFromGlob 、 addFilesFromRegexメソッドのシグネチャを変更しました。getLevelsetCompressionMethodsetEntryPassword