/** * Download file and return new pathname of extracted content * * The geoname's zipfile contains this structure: * - {countryCode}.txt * - readme.txt * * We're only interested in the first file. * * @param string $tempname temporary name for downloaded content * * @return string Pathname of the resulting file */ private function extractFile($tempname) { $extractedFiles = $this->extractor->extractFromFile($tempname)->files()->notName('readme.txt')->getIterator(); $extractedFiles->rewind(); if (!$extractedFiles->valid()) { throw new FileNotFoundException(); } return $extractedFiles->current(); }
/** * Download the data file from remote server and return a local file * * @param OutputInterface $output The output interface * @param string $countryCode Country code * @param boolean $sourcePackageMustbeReloaded Source package must be reloaded * * @return SplFileInfo File */ protected function downloadRemoteFileFromCountryCode(OutputInterface $output, $countryCode, $sourcePackageMustbeReloaded) { $filePath = $this->getDataFilePathFromCountryCode($countryCode); $temporaryDirPath = sys_get_temp_dir() . '/' . 'elcodi_geo/geodata/'; if (!is_dir($temporaryDirPath)) { mkdir($temporaryDirPath, 0777, true); } $temporaryFilePath = $temporaryDirPath . $countryCode . '.zip'; if (is_file($temporaryFilePath)) { $output->writeln('<header>[Geo]</header> <body>Source package found in cache</body>'); if (!$sourcePackageMustbeReloaded) { unlink($temporaryFilePath); $output->writeln('<header>[Geo]</header> <body>Cached Source package ignored</body>'); $output->writeln('<header>[Geo]</header> <body>Downloading source package from ' . $filePath . '</body>'); copy($filePath, $temporaryFilePath); $output->writeln('<header>[Geo]</header> <body>Downloaded source package to ' . $temporaryFilePath . '</body>'); } } else { $output->writeln('<header>[Geo]</header> <body>Downloading source package from ' . $filePath . '</body>'); copy($filePath, $temporaryFilePath); $output->writeln('<header>[Geo]</header> <body>Downloaded source package to ' . $temporaryFilePath . '</body>'); } $temporaryDirectory = new TemporaryDirectory(); $extensionResolver = new ExtensionResolver(); $extractor = new Extractor($temporaryDirectory, $extensionResolver); $filesIterator = $extractor->extractFromFile($temporaryFilePath)->files()->notName('readme.txt')->getIterator(); $filesArray = iterator_to_array($filesIterator); /** * @var SplFileInfo $file */ $file = reset($filesArray); $output->writeln('<header>[Geo]</header> <body>Found file ' . $file->getFilename() . '</body>'); return $file; }
/** * Download and read zip export * * @param array $params * @return \Symfony\Component\Finder\Finder */ public function getExportFiles(array $params) { $client = $this->getHttpClient(); if (empty($params['url'])) { throw new \InvalidArgumentException("Missing 'url' parameter! Must be the 'urlRecuperation' you got from the notification."); } if (preg_match('/\\.zip$/i', $params['url']) !== 1) { throw new \InvalidArgumentException("'url' parameter does not looks good! Must be the 'urlRecuperation' you got from the notification."); } $temporaryDirectory = $this->getExportDirectory(); $exportPath = sprintf('%s/%s', $temporaryDirectory->getDirectoryPath(), date('Y-m-d-His')); $zipFullPath = sprintf('%s/export.zip', $exportPath); $exportFullPath = sprintf('%s/export/', $exportPath); mkdir($exportPath); mkdir($exportFullPath); // Download the ZIP file in temp directory try { $response = $client->get($params['url'], ['stream' => true]); } catch (\Exception $e) { $this->handleHttpError($e); return false; } // Could use save_to too file_put_contents($zipFullPath, $response->getBody()); // Extract the ZIP file $extractor = new Extractor(new SpecificDirectory($exportFullPath)); return $extractor->extractFromFile($zipFullPath); }
/** * Extract files and get a Finder for month * * @param string $month * @return Finder * @author Fran Iglesias */ private function getAllPayrollFiles($paths, $month) { $extractor = new Extractor($this->createDirectory($month), new ExtensionResolver()); foreach ($paths as $path) { $extractor->extractFromFile($path); } return (new Finder())->files()->in($this->basePath . '/' . $month); }