Home  >  Article  >  Backend Development  >  How to read zip content in php? (Use of zip_entry_read function)

How to read zip content in php? (Use of zip_entry_read function)

藏色散人
藏色散人Original
2019-04-19 10:14:454482browse

This article mainly introduces how PHP obtains content from an opened zip file. There is a built-in function in PHP that can achieve this, namely the zip_entry_read() function.

How to read zip content in php? (Use of zip_entry_read function)

zip_entry_read() function is a built-in function in PHP that is used to read content from an opened zip archive entry. The zip entry is being read, the number of bytes returned can be sent as a parameter to the zip_entry_read() function, if successful it will return the contents of the specified zip entry, otherwise a PHP warning will be returned.

Syntax:

string zip_entry_read( $zip_entry, $length )

Parameters:

The function accepts two parameters, as described below.

$zip_entry: This is a mandatory parameter that specifies the zip entry resource.

$length: It is an optional parameter that specifies the number of bytes to be returned.

Return value:

Returns the contents of the specified zip entry if successful, otherwise returns a PHP warning.

Errors and Exceptions:

If the zip archive is invalid, the zip_entry_read() function will return an ER_OPEN error.

The zip_entry_read() function returns an ER_NOZIP error if the zip archive is empty

The following program demonstrates the zip_entry_read() function in PHP:

Example 1 :

Assume that the zip file article.zip contains the file: geeks.txt

<?php 
  
// 打开zip文件
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip"); 
   
// 读取zip存档项
while($zip_entry = zip_read($zip_handle))  
{  
    $resource = zip_entry_open($zip_handle, $zip_entry, "rb"); 
    $file_name = zip_entry_name($zip_entry); 
    
    if ($resource == true)  
    {  
   
        // 读取zip存档项的内容
        $file_content = zip_entry_read($zip_entry); 
        echo("File: " . $file_name . " successfully opened. <br>"); 
        echo("File content: " . $file_content); 
   
        // 关闭zip归档项
        zip_entry_close($zip_entry); 
    }  
    else
        echo("Failed to Open."); 
} 
  
// 关闭zip文件
zip_close($zip_handle); 
?>

Output:

File: articles/geeks successfully opened. 
File content: Welcome to GeeksforGeeks. It is a computer science portal
where you can learn programming.

Example 2:

Assume that the zip file article.zip contains the following files:

geeks.txt

geeks1.txt

<?php 
  
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip"); 
   
while($zip_entry = zip_read($zip_handle))  
{  
    $resource = zip_entry_open($zip_handle, $zip_entry, "rb"); 
    $file_name = zip_entry_name($zip_entry); 
    if ($resource == true)  
    {  
   
        // 读取zip存档项的内容,最多可达150字节
        $file_content = zip_entry_read($zip_entry, 150); 
        echo("File Name: " . $file_name . " is opened Successfully. <br>"); 
        echo($file_content); 
        echo("<br><br>"); 
  
       
        zip_entry_close($zip_entry); 
    }  
    else
        echo("Failed to Open."); 
}  
  
zip_close($zip_handle); 
?>

Output:

File Name: articles/geeks is opened Successfully. 
Welcome to GeeksforGeeks. It is a computer science portal where you
can learn programming.

File Name: articles/geeks1 is opened Successfully. 
A Computer Science portal for geeks. It contains well written, well
thought and well-explained computer science and programming articles,
quizzes and many more.

Related recommendations: "PHP Tutorial"

The above is the detailed content of How to read zip content in php? (Use of zip_entry_read function). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn