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


PHP zip_entry_open()用法及代碼示例


zip_entry_open()函數是PHP中的內置函數,用於打開zip條目存檔以供閱讀。使用zip_entry_open函數在zip存檔中打開文件或目錄會創建一個新的流,並在該流與Zip存檔中的文件或目錄之間建立連接。要打開的zip資源和zip條目資源,並將其作為參數發送給zip_entry_open()函數,成功時返回True,失敗時返回False。

用法:

bool zip_entry_open( $zip, $zip_entry, $mode )

參數:此函數接受上述和以下所述的三個參數:


  • $zip:它是必填參數,用於指定要讀取的zip資源。
  • $zip_entry:它是必填參數,用於指定zip條目資源。
  • $mode:這是一個可選參數,是zip歸檔文件所需的訪問類型。

返回值:成功返回True,失敗返回False。

錯誤和異常:

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

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

程序1:

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

<?php 
  
// Opening a zip file 
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip"); 
$zip_entry = zip_read($zip_handle); 
  
// Opening a zip entry archive  
zip_entry_open($zip_handle, $zip_entry, "rb"); 
$file = zip_entry_name($zip_entry); 
  
if($file == true) 
    echo("Zip file:" . $file . " open successfully <br>"); 
// Closing a zip entry archive  
$flag = zip_entry_close($zip_entry); 
if ($flag == true)  
    echo("Zip file:" . $file . " closed successfully"); 
else
    echo("Zip file:" . $file . " cannot be closed"); 
  
// Closeing zip file 
zip_close($zip_handle); 
?>

輸出:

Zip file:articles/geeks open successfully 
Zip file:articles/geeks closed successfully

程序2:

Suppose a zip file articles.zip contains the following files:
geeks.txt
geeks1.txt

<?php 
  
// Opening a zip file 
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip"); 
  
if(is_resource($zip_handle))  
{  
    while($zip_entry = zip_read($zip_handle))  
    {  
    
        // Opening a zip archive entry 
        $file = zip_entry_open($zip_handle, $zip_entry, "rb"); 
        $file_name = zip_entry_name($zip_entry); 
          
        if ($file == true)  
        {  
            echo("Zip file:" . $file_name . " open successfully"); 
            echo "<br>" ;  
    
            // Closing a zip archive entry 
            $flag = zip_entry_close($zip_entry); 
              
            if ($flag == true)  
                  echo("Zip file:" . $file_name . 
                      " closed successfully <br><br>"); 
            else
                echo("Zip file:" . $file_name .  
                         " cannot be closed <br><br>"); 
        }  
        else
            echo("Zip Entry Cannot be opened.<br>"); 
    }  
    
    // Closing a zip archive 
    zip_close($zip_handle); 
} 
else
    echo("Failed to Open" . $zip_handle ); 
?>

輸出:

Zip file:articles/geeks open successfully
Zip file:articles/geeks closed successfully 

Zip file:articles/geeks1 open successfully
Zip file:articles/geeks1 closed successfully

相關文章:

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



相關用法


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