如何在PHP中处理application / octet-stream(未压缩的gzip文件)?

时间:2012-12-12 23:27:56

标签: php gzip guzzle

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

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

    /** @var $guzzle \Guzzle\Http\Client */
    $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;
    }

3 个答案:

答案 0 :(得分:1)

存储正文的EntityBody对象只能猜测本地文件的内容类型。使用响应的content-length标头可以获得更准确的值。

这样的事情:

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

答案 1 :(得分:0)

某些shell命令之类的东西适用于你

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

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

作为旁注:

注意运行命令的位置(使用swith告诉“解压缩到该目录”) 您可能也想看看escapeshellarg; - )

答案 2 :(得分:0)

您应该能够使用内置的gzuncompress功能。

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

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

相关问题