如何在PHP中处理应用程序/八位字节流(未压缩的gziped文件)



我必须解析很多(10000+)的远程gzip文件。每个压缩文件都应包含CSV(可能在文件夹中)。现在我能够获取正文,检查内容类型并解压缩它,获得application/octet-stream.

问题是:什么是八位字节流,如何检查其中的文件或文件夹?

    /** @var $guzzle GuzzleHttpClient */
    $guzzle  = $this->getContainer()->get('guzzle');
    $request = $guzzle->get($url);
    try {
        $body = $request->send()->getBody();
        // Check for body content-type
        if('application/z-gzip' === $body->getContentType()) {
            $body->uncompress(); 
            $body->getContentType(); // application/octet-stream
        }
        else {
            // Log and skip current remote file
        }
    }
    catch(Exception $e) {
        $output->writeln("Failed: {$guzzle->getBaseUrl()}");
        throw $e;
    }

存储正文的 EntityBody 对象只能猜测本地文件的内容类型。使用响应的内容长度标头获取更准确的值。

像这样:

$response = $request->send();
$type = $response->getContentType();

像一些shell命令这样的东西会为你工作

shell_exec('gzip -d your_file.gz');

您可以先解压缩特定目录中的所有文件,然后可以读取每个文件或必须执行的任何计算。

作为旁注:

注意命令从何处运行(使用 swith 告诉"解压缩到该目录")你可能也想看看escapeshellarg ;-)

你应该能够使用内置的gzuncompress函数。

请参阅 http://php.net/manual/en/function.gzuncompress.php

编辑:或其他 zlib 函数,具体取决于您正在使用的数据。 http://php.net/manual/en/ref.zlib.php

最新更新