0

我必须解析很多(10000+)远程压缩文件。每个压缩文件都应该在其中包含一个 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;
    }
4

3 回答 3

1

存储body的EntityBody对象只能猜测本地文件的content-type。使用响应的 content-length 标头来获得更准确的值。

像这样的东西:

$response = $request->send();
$type = $response->getContentType();
于 2012-12-13T08:25:02.560 回答
0

像一些shell命令之类的东西对你有用

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

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

作为旁注:

注意从哪里运行命令(不要使用 swith 来告诉“解压缩到该目录”)您可能也想看看 escapeshellarg ;-)

于 2012-12-12T23:37:03.820 回答
0

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

http://php.net/manual/en/function.gzuncompress.php

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

于 2012-12-12T23:43:39.280 回答