當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP zip_entry_read()用法及代碼示例


zip_entry_read()函數是PHP中的內置函數,用於從打開的zip歸檔條目中讀取內容。正在讀取zip條目,並且可以將返回的字節數作為參數發送給zip_entry_read()函數,並在Success上返回指定zip條目的內容,否則返回PHP警告。

用法:

string zip_entry_read( $zip_entry, $length )

參數:該函數接受上麵提到的和下麵描述的兩個參數。


  • $zip_entry:它是必填參數,用於指定zip條目資源。
  • $length:它是一個可選參數,用於指定要返回的字節數。

返回值:它在成功時返回指定的zip條目的內容,否則返回PHP警告。

錯誤和異常:

  • 如果zip存檔無效,則zip_entry_read()函數將返回ER_OPEN錯誤。
  • 如果zip存檔為空,則zip_entry_read()函數返回ER_NOZIP錯誤。

以下示例程序旨在說明PHP中的zip_entry_read()函數:

程序1:

Suppose a zip file article.zip contains the following file:
geeks.txt

<?php 
  
// Opening a zip file 
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip"); 
   
// Reading a zip archive entry 
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)  
    {  
   
        // Reading contents of a zip archive entry 
        $file_content = zip_entry_read($zip_entry); 
        echo("File:" . $file_name . " successfully opened. <br>"); 
        echo("File content:" . $file_content); 
   
        // Closing a zip archive entry 
        zip_entry_close($zip_entry); 
    }  
    else
        echo("Failed to Open."); 
} 
  
// Closin zip file. 
zip_close($zip_handle); 
?>

輸出:

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

程序2:

Suppose a zip file article.zip contains the following file:

geeks.txt
geeks1.txt

<?php 
  
// Opening a zip file 
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip"); 
   
// Reading a zip archive entry 
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)  
    {  
   
        // Reading contents of a zip archive entry upto 150 bytes 
        $file_content = zip_entry_read($zip_entry, 150); 
        echo("File Name:" . $file_name . " is opened Successfully. <br>"); 
        echo($file_content); 
        echo("<br><br>"); 
  
        // Closing a zip archive entry 
        zip_entry_close($zip_entry); 
    }  
    else
        echo("Failed to Open."); 
}  
  
// Closing a zip archive 
zip_close($zip_handle); 
?>

輸出:

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. 

相關文章:

參考: http://php.net/manual/en/function.zip-entry-read.php



相關用法


注:本文由純淨天空篩選整理自Shubrodeep Banerjee大神的英文原創作品 PHP | zip_entry_read() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。