php如何读取zip内容?(zip_entry_read函数的使用)

php如何读取zip内容?(zip_entry_read函数的使用)

内容导读

收集整理的这篇技术教程文章主要介绍了php如何读取zip内容?(zip_entry_read函数的使用),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2532字,纯文字阅读大概需要4分钟

内容图文

本篇文章主要给大家介绍PHP如何从打开的 zip 档案中获取内容,那么在PHP中有一个内置函数可以实现,即zip_entry_read()函数。

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 portalwhere 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 youcan learn programming.File Name: articles/geeks1 is opened Successfully. A Computer Science portal for geeks. It contains well written, wellthought and well-explained computer science and programming articles,quizzes and many more.

相关推荐:《PHP教程》

以上就是php如何读取zip内容?(zip_entry_read函数的使用)的详细内容,更多请关注Gxl网其它相关文章!

内容总结

以上是为您收集整理的php如何读取zip内容?(zip_entry_read函数的使用)全部内容,希望文章能够帮你解决php如何读取zip内容?(zip_entry_read函数的使用)所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。

内容备注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。


本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com