php里zip,在PHP中读取zip文件的最佳方法

这段代码演示了如何使用PHP的zip_open和zip_read函数从ZIP归档中提取一个目录。它首先打开ZIP文件,然后遍历每个条目,创建必要的目录结构,并将文件写入目标位置。如果遇到空目录,则创建相应的目录。最后,关闭ZIP文件。此功能适用于解压特定路径内的ZIP文件。
摘要由CSDN通过智能技术生成

使用zip_open和zip_read函数来完成它.

您可以在

http://php.net/manual/en/function.zip-read.php找到它的文档

/**

* This method unzips a directory within a zip-archive

*

* @author Florian 'x!sign.dll' Wolf

* @license LGPL v2 or later

* @link http://www.xsigndll.de

* @link http://www.clansuite.com

*/

function extractZip( $zipFile = '', $dirFromZip = '' )

{

define(DIRECTORY_SEPARATOR, '/');

$zipDir = getcwd() . DIRECTORY_SEPARATOR;

$zip = zip_open($zipDir.$zipFile);

if ($zip)

{

while ($zip_entry = zip_read($zip))

{

$completePath = $zipDir . dirname(zip_entry_name($zip_entry));

$completeName = $zipDir . zip_entry_name($zip_entry);

// Walk through path to create non existing directories

// This won't apply to empty directories ! They are created further below

if(!file_exists($completePath) && preg_match( '#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry)) ) )

{

$tmp = '';

foreach(explode('/',$completePath) AS $k)

{

$tmp .= $k.'/';

if(!file_exists($tmp) )

{

@mkdir($tmp, 0777);

}

}

}

if (zip_entry_open($zip, $zip_entry, "r"))

{

if( preg_match( '#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry)) ) )

{

if ($fd = @fopen($completeName, 'w+'))

{

fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));

fclose($fd);

}

else

{

// We think this was an empty directory

mkdir($completeName, 0777);

}

zip_entry_close($zip_entry);

}

}

}

zip_close($zip);

}

return true;

}

// The call to exctract a path within the zip file

extractZip( 'clansuite.zip', 'core/filters' );

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值