【问题标题】:PHP ZipArchive large ExtractTo with foldersPHP ZipArchive 带有文件夹的大型 ExtractTo
【发布时间】:2012-09-14 12:03:01
【问题描述】:

我在使用 ZipArchive extractTo 时遇到问题。

我有一个 +300Mb 的 ZIP 文件,其中包含 100 个文件夹和 +3k XML 文件。当我开始这个过程时,它会运行到 20 个文件夹和内部档案并停止工作。

这是我的解压功能...

public function unzip_files($zipfile, $parent_folder)
{
    ini_set('memory_limit', '512M');
    set_time_limit(0);      

    $zip = new ZipArchive;
    $res = $zip->open($zipfile);

    if( $res === true )
    {
        if( $zip->extractTo(HCAT_UPLOADS . $parent_folder) );
        {
            $zip->close();

            print '<strong>'. basename($zipfile) .'</strong> '. __('unziped correctly', self::$ltd) .'.<br />';

            return true;
        }
        else
        {
            print __('Failed to unzip', self::$ltd) .' <strong>'. basename($zipfile) .'</strong>.<br />';

            return false;
        }
    }
    else
    {
        print __('Failed to unzip', self::$ltd) .' <strong>'. basename($zipfile) .'</strong>.<br />';

        return false;
    }
}

如何解压缩所有文件夹?有什么提示吗? :)

谢谢!
R

【问题讨论】:

    标签: php zip limit unzip ziparchive


    【解决方案1】:

    ZipArchive 将 ExtractTo 限制为 65535 个文件,并且无法进行偏移。

    所以,顺便说一句,最好的解决方法是使用 shell 命令:

    public function unzip_files($zipfile, $parent_folder)
    {
        $disableds = explode(', ', ini_get('disable_functions'));
    
        if( !in_array('exec', $disableds) )
        {
            exec("unzip -o $zipfile -x -d $parent_folder");
    
            print '<strong>'. basename($zipfile) .'</strong> '. __('unziped correctly', self::$ltd) .'.<br />';
        }
    }
    

    最好!
    R

    【讨论】:

    • 不仅仅是 ExtractTo - PHP 5.3.3 似乎有一个 ZipArchive 无法处理超过 65k 个文件的内部限制。
    【解决方案2】:

    它对我有用..!!因为我们的一个应用程序在 PHP 5.3 - extractTo() 中运行,这不允许我们上传超过 65KB 的 ZIP 文件。

    exec("unzip -o $zipFileName -x -d $uploadedPath");

    例子:

    $zip_obj = new ZipArchive();
    $zip_obj_data = $zip_obj->open($zipFileName);
    if ($zip_obj_data === true) {
        #$zip_obj->extractTo($uploaded_path);
        #$zip_obj->close();
        $disableds = explode(', ', ini_get('disable_functions'));
        if( !in_array('exec', $disableds) )
        {
            $zipfile = $zipFileName;
            exec("unzip -o $zipfile -x -d $uploaded_path");
        } 
        unlink($zipFileName);
    
    }    
    

    注意:'exec' 命令不属于 PHP 安全组,在您赢得风险时使用此命令。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多