ホームページ  >  記事  >  バックエンド開発  >  PHP zipアーカイブ

PHP zipアーカイブ

PHPz
PHPzオリジナル
2024-08-29 13:00:23380ブラウズ

PHP の ZipArchive メソッドは、ファイル、新しいディレクトリを追加し、PHP で zip を読み取るために使用されます。 ZipArchive はメソッドではなくクラスです。 ziparchive には複数のメソッドが含まれています。これを使用すると、PHP で zip に対してさまざまな操作を実行できます。 ZipArchive メソッドは、新しいファイルの追加、新しいディレクトリの追加、アーカイブを閉じる、zip アーカイブのコンテンツを抽出する、PHP で zip アーカイブを開くなどの複数の操作を実行できます。次のセクションでは、PHP で ziparchive メソッドを使用する方法を詳しく見ていきます。

広告 このカテゴリーの人気コース HIVE - 専門分野 | 7 コース シリーズ

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文

ここで、PHP における ziparchive の複数の構文について説明します。先ほども述べたように、PHP には ziparchive のようなメソッドはありません。これは代わりに、zip にアクセスして zip を読み取るためのいくつかの ziparchive メソッドをその中に含むクラスです。以下を参照して、メソッドをよりよく理解するためにいくつかの構文を見てみましょう。

1. ziparchive を開くには: 以下の構文

ziparchive->open('your_file_name');

ご覧のとおり、開きたいファイル名を渡すだけです。また、これらのメソッドは、ziparchive インスタンスを使用して呼び出すこともできます。

2.ファイルを閉じるには: 以下の構文

ziparchive->close();

ご覧のとおり、PHP でファイルを閉じるには、ziparchive インスタンスで close メソッドを呼び出すだけです。

PHP での ziparchive メソッドはどのように機能しますか?

ご存じのとおり、PHP で zip を読み取るために ziparchive メソッドが使用されることは知られていますが、ziparchive はメソッドではなくクラスではなく、ziparchive で操作を実行するために使用できるメソッドがいくつか含まれています。したがって、ziparchive を処理するには、PHP の ziparchive クラスを使用します。いずれのメソッドを使用する場合も ziparchive クラスのインスタンスを作成する必要がありますが、これを利用することで簡単に任意のメソッドを呼び出すことができます。 ziparchive インスタンスを使用して、ファイルを開くなどの操作を実行できます。ファイルを閉じる、新しいファイルを作成する、新しいディレクトリを作成する、zipアーカイブのコンテンツを読み取るなどです。

PHP で使用する各メソッドについて詳しく説明します。以下を参照してください。

  1. statIndex: This method is used to get the entry of the file defined by using the index.
  2. setCompressionIndex: This method is used to define the comparison method.
  3. renameName: This method is used to rename the file.
  4. open: This method is used to open a zip file in PHP.
  5. setCommentName: This method is used to comment.
  6. unchangeAll: This method is used to revert all the changes that have been performed to the zip archive.
  7. setPassword: This method is issued to set the password for the zip archive.
  8. close: This method is used to close the zip archive but is currently being used.
  9. addEmptyDir: This method is used to add a new empty directory to the zip archive.
  10. addFromString: This method is used to a file to the zip archive.
  11. count: This method is used to count the number of files present in the archive.
  12. deleteName: This method is used to delete an entry from the zip archive based on the name.
  13. addFile: This method is issued to add a file in the zip archive. But we have to specify the path as well.
  14. statName: This method is used to get the details of the entry based on the name.
  15. replaceFile: This method is used to replace the file in the zip archive based on the path.
  16. locateName: This method is used to get the index of the entry in the zip archive.
  17. getNameIndex: This method is used to get the name of the entry from the zip archive based on the index.
  18. getCommentName: This method is used to get the comment of the entry based on the name.
  19. getFromIndex: This method is used to get the contents of the entry by sing its index in a zip archive.
  20. extractTo: This method is used to get or extract the contents of the zip archive in PHP.
  21. deleteIndex: This method is used to delete the entry from the zip archive by using the index.
  22. setEncryptionIndex: This method is used to set the encryption for the entry in the zip archive using its index.
  23. setArchiveComment: This method is used to set comments for the zip archive in PHP.
  24. setExternalAttributesName: This method is used to set the external attribute of the entry based on its name.
  25. setEncryptionName: This method is used to set the encryption of the entry of the zip archive based on the name.

To use this all this method, we need to have a ziparchive instance created in our program. After that, only we can call this method to perform any operations on the zip archive in PHP. Let’s see how to create a ziparchive instance in PHP see below;

Example:

$myzip = new ZipArchive();

This is s sample for beginners to use ziparchive in php; we will see some examples in the next section to better understand the ziparchive method in PHP.

Example of PHP ziparchive

In this example, we create one file inside the zip archive; after this, we are closing this file. A sample program for beginners to understand this better.

Code:

<?php
echo 'demo for ziparchive method in PHP !!!';
$zip = new ZipArchive;
echo 'opening zip file !!';
if ($zip->open('https://cdn.educba.com/path/mydem.zip') === TRUE) {
//adding file
$zip->addFile('https://cdn.educba.com/path/myzip/demofile.txt', 'mynewdemofile.txt');
//closing file
$zip->close();
echo 'file added successfully !!';
echo 'file close successfully !!'
} else {
echo 'error occured while cerating the file.'
echo 'process failed !!'
echo 'failed while creation !!';
}
?>

Output:

PHP zipアーカイブ

Conclusion

By using ziparchive methods, we can perform many operations on the ziparchive. This is the class with so many different methods available. With the help of this, we can create a file, directory, delete, open, and close our ziparchive in PHP.

以上がPHP zipアーカイブの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:PHP 名前空間次の記事:PHP 名前空間