>  기사  >  백엔드 개발  >  PHP 압축 아카이브

PHP 압축 아카이브

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와 같은 방법이 없습니다. 이는 zip에 액세스하고 zip을 읽을 수 있는 여러 ziparchive 메서드가 내부에 포함된 클래스입니다. 아래에서 메서드를 더 잘 이해하기 위해 몇 가지 구문을 살펴보겠습니다.

1. ziparchive를 열려면: 다음은 구문입니다

ziparchive->open('your_file_name');

보시다시피 열려고 하는 파일 이름만 전달하면 됩니다. 또한 ziparchive 인스턴스를 사용하여 이러한 메서드를 호출할 수 있습니다.

2. 파일을 닫으려면: 다음은 구문입니다

ziparchive->close();

보시다시피, PHP에서 모든 파일을 닫으려면 ziparchive 인스턴스에서 close 메소드를 호출하기만 하면 됩니다.

PHP에서 ziparchive 메소드는 어떻게 작동하나요?

아시다시피 ziparchive 메소드는 PHP에서 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 압축 아카이브

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 압축 아카이브의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP 네임스페이스다음 기사:PHP 네임스페이스