2012-12-12 388 views
-1

我必須解析很多(10000+)遠程gzip文件。每個壓縮文件都應該包含一個CSV文件(可能位於一個文件夾中)。現在我可以獲取主體,檢查內容類型並解壓縮,獲取application/octet-stream如何在PHP中處理application/octet-stream(未壓縮的gzip文件)?

問題是:什麼是八位字節流,我如何檢查裏面的文件或文件夾?

/** @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; 
    } 
+0

沒有任何答案的協助之下這個老問題,@gremo? – halfer

回答

0

東西像一些shell命令將努力爲ü

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

您可以先解壓在一個特定的目錄中的所有文件,然後可以讀取每個文件或任何你必須執行計算。

一點題外話:

取其中命令是從(OT使用開關等來告訴「解壓縮到該目錄」)運行護理 你可能想看看escapeshellarg太;-)

+0

沒有選項... – gremo

1

存儲主體的EntityBody對象只能猜測本地文件的內容類型。使用響應的內容長度標題可以獲得更準確的值。

事情是這樣的:

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