温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php读取zip内容的方法

发布时间:2020-08-24 16:51:19 来源:亿速云 阅读:109 作者:小新 栏目:编程语言

这篇文章主要介绍php读取zip内容的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

zip_entry_read()函数是PHP中内置的函数,用于从打开的zip归档条目中读取内容。正在读取zip条目,返回的字节数可以作为参数发送给zip_entry_read()函数,如果成功,它将返回指定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:

假设zip文件article.zip包含文件: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); 
?>

输出:

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

示例2:

假设zip文件article.zip包含以下文件:

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); 
?>

输出:

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.

以上是php读取zip内容的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI