首頁  >  文章  >  後端開發  >  PHP zip壓縮包

PHP zip壓縮包

PHPz
PHPz原創
2024-08-29 13:00:23380瀏覽

PHP中的ZipArchive方法用於新增檔案、新目錄,並能夠在PHP中讀取zip。 ZipArchive 是一個類別而不是一個方法; ziparchive 包含多種方法;透過使用它,我們可以在 PHP 中對 zip 執行各種操作。 ZipArchive 方法可以執行多種操作,包括新增檔案、新增目錄、關閉檔案、提取 ziparchive 內容、在 PHP 中開啟 ziparchive。在接下來的部分中,我們將詳細了解如何在 PHP 中使用 ziparchive 方法。

廣告 該類別中的熱門課程 HIVE - 專業化 | 7門課程系列

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

現在我們將討論 PHP 中 ziparchive 的多種語法。正如我告訴過你的,PHP 中沒有 ziparchive 這樣的方法;這是一個類,其中包含多個 ziparchive 方法來存取 zip 並讀取 zip。讓我們來看一些語法,以便更好地理解該方法,如下所示;

1.要開啟 ziparchive:以下是語法

ziparchive->open('your_file_name');

如您所見,我們只需傳遞要開啟的檔案名稱即可。此外,可以使用 ziparchive 實例呼叫這些方法。

2.關閉檔案:以下是語法

ziparchive->close();

如您所見,我們只需呼叫 ziparchive 實例上的 close 方法即可關閉 PHP 中的任何檔案。

ziparchive 方法在 PHP 中如何運作?

眾所周知,我們知道PHP中使用ziparchive方法來讀取zip,但是ziparchive不是一個類,不是一個方法,而是包含了幾個可以用來對ziparchive進行操作的方法。因此,為了處理 ziparchive,我們在 PHP 中使用 ziparchive 類別。要使用任何方法,我們必須建立 ziparchive 類別的實例,但透過使用它,我們可以輕鬆呼叫任何方法。我們可以使用 ziparchive 實例執行任何操作,例如開啟檔案。關閉檔案、建立新檔案、建立新目錄、讀取 ziparchive 內容等等。

讓我們詳細討論每種方法,以便在 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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP命名空間下一篇:PHP命名空間