简体   繁体   English

PHP-异步提取大型ZIP文件

[英]PHP - Extract large ZIP file asynchronously

I'm struggling with this problem for the last few days so maybe somebody could help me with this. 最近几天我一直在努力解决这个问题,所以也许有人可以帮助我解决这个问题。 I'm developing a large app that has to extract one ~200MB ZIP file containing about 3-4GB of CSV files. 我正在开发一个大型应用程序,该应用程序必须提取一个〜200MB的ZIP文件,其中包含约3-4GB的CSV文件。

For that I've written a jQuery plugin that was supposed to show something to the user while the ZIP is extracting, and to handle timeouts. 为此,我编写了一个jQuery插件,该插件应该在ZIP提取时向用户显示一些内容并处理超时。 But once the extraction process has started, the Ajax cannot send "has it finished yet?" 但是一旦提取过程开始,Ajax就无法发送“它完成了吗?” requests to the server, as they also time out. 向服务器的请求,因为它们也会超时。 So my question is - is there a way to start the ZIP extraction asynchronously and then check the progress? 所以我的问题是-有没有一种方法可以异步启动ZIP提取,然后检查进度?

I'm building this app on CodeIgniter framework and the zip extraction function looks like this: 我正在CodeIgniter框架上构建此应用,并且zip提取功能如下所示:

public function unpack_zip ($zip_file) {
    // First we check the database to see if the extraction process has already started
    $extracting_file = $this->db->select('value')->from('setting')->where(array('type' => 0, 'key' => 'tube_extractng'))->get()->result_array();

    if (!!$extracting_file[0]['value']) return 'Already extracting!';

    // Next we create a strimg containing path to the zip file.
    $source_file = $this->install_assets_dir . 'zip' . DIRECTORY_SEPARATOR . $zip_file;

    $zip = new ZipArchive;

    if ($zip->open($source_file) === TRUE) {
            // If the zip was open sucessfully we tell the database that extraction is in progress, this stays "true" until the process completes
        $this->db->update('setting', array('value' => true), array('type' => 0, 'key' => 'tube_extractng'));

        set_time_limit(600); // The timeout long enough to extract an extra large file
        $zip->extractTo($this->install_assets_dir . 'csv' . DIRECTORY_SEPARATOR);
        $zip->close();

        rename($source_file, preg_replace('/\.zip$/', '.extracted$0', $source_file)); // Renaming the extracted zip by adding ".extracted" before the extension
        $this->db->update('setting', array('value' => false), array('type' => 0, 'key' => 'tube_extractng')); // And, of course tell the database that the process is done.
        return TRUE;
    }

    return FALSE;

}

The problem is - once the process starts the server is unresponsive until the extraction is complete. 问题是-进程启动后,服务器将无响应,直到提取完成。 Unresponsive server = no responses to "are you still extracting?" 服务器无响应=对“您还在提取吗?”无响应 Ajax calls. Ajax调用。

After digging into it more and more I found out where the issue was. 在深入研究之后,我发现了问题所在。 I'm using WT-NMP and my PHP was set to 1 PHP-CGI server, which in effect blocks all other requests when one is in progress. 我使用的是WT-NMP,而我的PHP设置为1个PHP-CGI服务器,当一个请求正在进行时,它实际上会阻止所有其他请求。 Setting that number to 2 already started giving responses to the "are you done?" 将该数字设置为2已经开始响应“您完成了吗?” ajax requests. ajax请求。 The thing now works as it was imagined. 事情现在像想象中的那样起作用。

So for anybody else with similar problem - first check the number of PHP CGI servers you're running. 因此,对于其他有类似问题的人,请先检查您正在运行的PHP CGI服务器的数量。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM