加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

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

发布时间:2020-05-25 09:32:49 所属栏目:PHP 来源:互联网
导读: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 wit

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

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读