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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了在PHP中读取zip文件的最佳方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
file_get_contents(“zip:///a/b/c.zip”)返回NULL.如何在 PHP 5中阅读zip文件的解压缩内容
使用zip_oPEn和zip_read函数来完成它.
您可以在 http://php.net/manual/en/function.zip-read.php找到它的文档
<?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' );
?>

脚本宝典总结

以上是脚本宝典为你收集整理的在PHP中读取zip文件的最佳方法全部内容,希望文章能够帮你解决在PHP中读取zip文件的最佳方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。