/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln(sprintf("distill-cli %s by Raul Fraile.\n", $this->appVersion)); $output->write(sprintf("Uncompressing '%s' into '%s' ", $input->getArgument('file'), $input->getArgument('target'))); $distill = new Distill(); $response = $distill->extract($input->getArgument('file'), $input->getArgument('target')); if (true == $response) { $output->writeln('[<info>OK</info>]'); return 0; } $output->writeln('<error>ERROR</error>'); return 1; }
/** * Extracts the compressed file (ZIP or TGZ) using the * native operating system commands if available or PHP code otherwise. * * @return DownloadCommand * * @throws \RuntimeException if the downloaded archive could not be extracted */ protected function extract() { $this->output->writeln(sprintf(" Extracting <info>%s</info>\n", $this->projectName)); try { $distill = new Distill(); $extractionSucceeded = $distill->extractWithoutRootDirectory($this->downloadedFilePath, $this->projectDir); } catch (FileCorruptedException $e) { throw new \RuntimeException(sprintf("%s can't be installed because the downloaded package is corrupted.\n" . "To solve this issue, try executing this command again:\n%s", $this->getDownloadedFileName(), $this->getExecutedCommand())); } catch (FileEmptyException $e) { throw new \RuntimeException(sprintf("%s can't be installed because the downloaded package is empty.\n" . "To solve this issue, try executing this command again:\n%s", $this->getDownloadedFileName(), $this->getExecutedCommand())); } catch (TargetDirectoryNotWritableException $e) { throw new \RuntimeException(sprintf("%s can't be installed because the installer doesn't have enough\n" . "permissions to uncompress and rename the package contents.\n" . "To solve this issue, check the permissions of the %s directory and\n" . "try executing this command again:\n%s", $this->getDownloadedFileName(), getcwd(), $this->getExecutedCommand())); } catch (\Exception $e) { throw new \RuntimeException(sprintf("%s can't be installed because the downloaded package is corrupted\n" . "or because the installer doesn't have enough permissions to uncompress and\n" . "rename the package contents.\n" . "To solve this issue, check the permissions of the %s directory and\n" . "try executing this command again:\n%s", $this->getDownloadedFileName(), getcwd(), $this->getExecutedCommand())); } if (!$extractionSucceeded) { throw new \RuntimeException(sprintf("%s can't be installed because the downloaded package is corrupted\n" . "or because the uncompress commands of your operating system didn't work.", $this->getDownloadedFileName())); } return $this; }
public function testFormatNotSupportedAfterDisablingAllMethods() { $this->setExpectedException('Distill\\Exception\\IO\\Input\\FileFormatNotSupportedException'); $target = $this->getTemporaryPath(); $this->clearTemporaryPath(); $this->distill->disableMethod(Cabextract::getName()); $this->distill->disableMethod(Gcab::getName()); $this->distill->disableMethod(X7zip::getName()); $result = $this->distill->extract($this->filesPath . 'file_ok.cab', $target, new Format\Simple\Cab()); $this->clearTemporaryPath(); }
<?php require __DIR__ . '/../vendor/autoload.php'; use Distill\Distill; use Distill\Strategy\MinimumSize; $distill = new Distill(); $strategy = new MinimumSize(); $preferredFile = $distill->getChooser()->setStrategy($strategy)->addFile('http://get.symfony.com/Symfony_Standard_Vendors_2.5.3.tgz')->addFile('http://get.symfony.com/Symfony_Standard_Vendors_2.5.3.zip')->getPreferredFile(); echo $preferredFile . \PHP_EOL; $preferredFile = $distill->getChooser()->setStrategy($strategy)->addFilesWithDifferentExtensions('http://get.symfony.com/Symfony_Standard_Vendors_2.5.3', ['zip', 'tgz'])->getPreferredFile(); echo $preferredFile . \PHP_EOL;
/** * Extracts the compressed Symfony file (ZIP or TGZ) using the * native operating system commands if available or PHP code otherwise. * * @throws \RuntimeException if the downloaded archive could not be extracted */ private function extract() { $this->output->writeln(" Preparing new core version...\n"); $this->uncompressedFilePath = dirname($this->compressedFilePath); try { $distill = new Distill(); $extractionSucceeded = $distill->extractWithoutRootDirectory($this->compressedFilePath, $this->uncompressedFilePath); } catch (FileCorruptedException $e) { throw new \RuntimeException(sprintf("ProcessWire can't be installed because the downloaded package is corrupted.\n" . "To solve this issue, try installing ProcessWire again.\n%s", $this->getExecutedCommand())); } catch (FileEmptyException $e) { throw new \RuntimeException(sprintf("ProcessWire can't be installed because the downloaded package is empty.\n" . "To solve this issue, try installing ProcessWire again.\n%s", $this->getExecutedCommand())); } catch (TargetDirectoryNotWritableException $e) { throw new \RuntimeException(sprintf("ProcessWire can't be installed because the installer doesn't have enough\n" . "permissions to uncompress and rename the package contents.\n" . "To solve this issue, check the permissions of the %s directory and\n" . "try installing ProcessWire again.\n%s", getcwd(), $this->getExecutedCommand())); } catch (\Exception $e) { throw new \RuntimeException(sprintf("ProcessWire can't be installed because the downloaded package is corrupted\n" . "or because the installer doesn't have enough permissions to uncompress and\n" . "rename the package contents.\n" . "To solve this issue, check the permissions of the %s directory and\n" . "try installing ProcessWire again.\n%s", getcwd(), $this->getExecutedCommand())); } if (!$extractionSucceeded) { throw new \RuntimeException("ProcessWire can't be installed because the downloaded package is corrupted\n" . "or because the uncompress commands of your operating system didn't work."); } return $this; }
/** * Lists all Module models. * * @return mixed */ public function actionUpload() { $model = new ThemezipForm(); if (\Yii::$app->getRequest()->getIsPost() == true && ($uploaded = UploadedFile::getInstance($model, "themezip")) != null) { $distill = new Distill(); $extractFileName = dirname($uploaded->tempName) . DIRECTORY_SEPARATOR . $uploaded->name; if (move_uploaded_file($uploaded->tempName, $extractFileName) == true) { $target = dirname($uploaded->tempName) . DIRECTORY_SEPARATOR . md5($extractFileName . time()); mkdir($target); if ($distill->extract($extractFileName, $target)) { $newTheme = \Yii::$app->get("themeManager")->findByPath($target . DIRECTORY_SEPARATOR . $uploaded->getBaseName()); if (count($newTheme) === 1) { $theme = \Yii::$app->get("themeManager")->findOne($newTheme->getPackage()); if ($theme == null) { $themePath = Yii::getAlias(\Yii::$app->get("themeManager")->getThemePath()); if ($distill->extract($extractFileName, $themePath)) { $this->flash("success", "上传主题文件成功"); } } else { $this->flash("error", "主题路径已经存在该主题ID"); } } else { $this->flash("error", "上传主题配置文件不存在或者上传主题有错误"); } } else { $this->flash("error", "解压文件失败"); } } else { $this->flash("error", "移动文件失败,请确定你的临时目录是可写的"); } if (file_exists($uploaded->tempName)) { unlink($uploaded->tempName); } if (file_exists($extractFileName)) { unlink($extractFileName); } FileHelper::removeDirectory($target); return $this->refresh(); } return $this->render('upload', ["model" => $model]); }
/** * Extracts the downloaded archive. */ protected function extract() { try { $distill = new Distill(); $extractionSucceeded = $distill->extractWithoutRootDirectory($this->majoraDownloader->getDestinationFile(), $this->destinationPath); } catch (FileCorruptedException $e) { $this->clean(true); throw new \RuntimeException(sprintf("Majora Standard Edition can't be installed because the downloaded package is corrupted")); } catch (FileEmptyException $e) { $this->clean(true); throw new \RuntimeException(sprintf("Majora Standard Edition can't be installed because the downloaded package is empty")); } catch (TargetDirectoryNotWritableException $e) { $this->clean(true); throw new \RuntimeException(sprintf("Majora Standard Edition can't be installed because the installer doesn't have enough\n" . 'permissions to uncompress and rename the package contents.')); } catch (\Exception $e) { $this->clean(true); throw new \RuntimeException(sprintf("Majora Standard Edition can't be installed because the downloaded package is corrupted\n" . "or because the installer doesn't have enough permissions to uncompress and\n" . "rename the package contents.\n" . 'To solve this issue, check the permissions of the %s directory', getcwd()), null, $e); } if (!$extractionSucceeded) { $this->clean(true); throw new \RuntimeException(sprintf("Majora Standard Edition can't be installed because the downloaded package is corrupted\n" . "or because the uncompress commands of your operating system didn't work.")); } }
<?php require __DIR__ . '/../vendor/autoload.php'; use Distill\Distill; $distill = new Distill(); $distill->extractWithoutRootDirectory(__DIR__ . '/../tests/files/file_ok_dir.tar.gz', __DIR__ . '/extract');
/** * Extracts the archive * * @param OutputInterface $output * @param $file * @param $target * @param $oxidVersion */ protected function extractArchive(OutputInterface $output, $file, $target, $oxidVersion) { $distill = new Distill(); $output->writeLn("Extracting"); $distill->extract($file, $target); $filesystem = new Filesystem(); $filesystem->mirror($target . '/' . $oxidVersion['folder'] . '/source', $target); $filesystem->remove($target . '/' . $oxidVersion['folder']); $pkgInfo = <<<EOT [Package info] revision = "{$oxidVersion['hash']}" edition = "CE" version = "{$oxidVersion['versionTag']}" encoder-version = "" timestamp = "{timestamp()}" EOT; file_put_contents($target . '/pkg.info', $pkgInfo); }
private function extractProfile($profile) { if (!$profile) { return $this; } $this->output->writeln(" Extracting profile...\n"); try { $distill = new Distill(); $extractPath = getcwd() . DIRECTORY_SEPARATOR . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . 'pwprofile'; $extractionSucceeded = $distill->extractWithoutRootDirectory($profile, $extractPath); if ($extractionSucceeded) { try { $this->fs->mirror($extractPath, $this->projectDir . '/'); } catch (\Exception $e) { } // cleanup $this->fs->remove($extractPath); try { $process = new Process("cd {$this->projectDir}; composer install"); $process->run(function ($type, $buffer) { if (Process::ERR === $type) { echo ' ' . $buffer; } else { echo ' ' . $buffer; } }); } catch (\Exception $e) { } } } catch (FileCorruptedException $e) { throw new \RuntimeException("The profile can't be installed because the downloaded package is corrupted.\n"); } catch (FileEmptyException $e) { throw new \RuntimeException("The profile can't be installed because the downloaded package is empty.\n"); } catch (TargetDirectoryNotWritableException $e) { throw new \RuntimeException("The profile can't be installed because the installer doesn't have enough\n" . "permissions to uncompress and rename the package contents.\n"); } catch (\Exception $e) { throw new \RuntimeException("The profile can't be installed because the downloaded package is corrupted\n" . "or because the installer doesn't have enough permissions to uncompress and\n" . "rename the package contents.\n" . $e->getMessage()); } if (!$extractionSucceeded) { throw new \RuntimeException("The profile can't be installed because the downloaded package is corrupted\n" . "or because the uncompress commands of your operating system didn't work."); } return $this; }
/** * Extracts the archive * * @param OutputInterface $output * @param $file * @param $target * @param $oxidVersion */ protected function extractArchive(OutputInterface $output, $file, $target, $oxidVersion) { $distill = new Distill(); $output->writeLn("Extracting"); $distill->extract($file, $target); $filesystem = new Filesystem(); $filesystem->mirror($target . '/' . $oxidVersion['folder'] . '/source', $target); $filesystem->remove($target . '/' . $oxidVersion['folder']); $oxidVersion['timestamp'] = date('Y-m-d H:i:s'); $oxidVersion['build'] = preg_replace('/[^\\d]+/', '', $oxidVersion['versionTag']); $pkgInfo = <<<EOT [Builder] build = {$oxidVersion['build']} [Package info] revision = "{$oxidVersion['sha']}" edition = "CE" version = "{$oxidVersion['versionTag']}" encoder-version = "" timestamp = "{$oxidVersion['timestamp']}" EOT; file_put_contents($target . '/pkg.info', $pkgInfo); file_put_contents($target . '/pkg.rev', $oxidVersion['sha']); }
/** * Extracts the compressed Symfony file (ZIP or TGZ) using the * native operating system commands if available or PHP code otherwise. * * param string $module * @return NewCommand * * @throws \RuntimeException if the downloaded archive could not be extracted */ private function extract($module) { $this->output->writeln(" Preparing module...\n"); try { $distill = new Distill(); $extractionSucceeded = $distill->extractWithoutRootDirectory($this->compressedFilePath, wire('config')->paths->siteModules . $module); $dir = wire('config')->paths->siteModules . $module; if (is_dir($dir)) { chmod($dir, 0755); } } catch (FileCorruptedException $e) { throw new \RuntimeException("This module can't be downloaded because the downloaded package is corrupted.\n" . "To solve this issue, try installing the module again.\n"); } catch (FileEmptyException $e) { throw new \RuntimeException("This module can't be downloaded because the downloaded package is empty.\n" . "To solve this issue, try installing the module again.\n"); } catch (TargetDirectoryNotWritableException $e) { throw new \RuntimeException(sprintf("This module can't be downloaded because the installer doesn't have enough\n" . "permissions to uncompress and rename the package contents.\n" . "To solve this issue, check the permissions of the %s directory and\n" . "try installing this module again.\n", getcwd())); } catch (\Exception $e) { throw new \RuntimeException(sprintf("This module can't be downloaded because the downloaded package is corrupted\n" . "or because the installer doesn't have enough permissions to uncompress and\n" . "rename the package contents.\n" . "To solve this issue, check the permissions of the %s directory and\n" . "try installing this module again.\n", getcwd())); } if (!$extractionSucceeded) { throw new \RuntimeException("This module can't be downloaded because the downloaded package is corrupted\n" . "or because the uncompress commands of your operating system didn't work."); } return $this; }
/** * Chooses the best compressed file format to download (ZIP or TGZ) depending upon the * available operating system uncompressing commands and the enabled PHP extensions * and it downloads the file. * * @param string $url * @param string $module * @param OutputInterface $output * @return NewCommand * * @throws \RuntimeException if the ProcessWire archive could not be downloaded */ public function downloadModule($url, $module, $output) { $output->writeln(" Downloading module {$module}..."); $distill = new Distill(); $pwArchiveFile = $distill->getChooser()->setStrategy(new MinimumSize())->addFile($url)->getPreferredFile(); /** @var ProgressBar|null $progressBar */ $progressBar = null; $downloadCallback = function ($size, $downloaded, $client, $request, Response $response) use(&$progressBar, &$output) { // Don't initialize the progress bar for redirects as the size is much smaller if ($response->getStatusCode() >= 300) { return; } if (null === $progressBar) { ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) { return $this->formatSize($bar->getMaxSteps()); }); ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) { return str_pad($this->formatSize($bar->getStep()), 11, ' ', STR_PAD_LEFT); }); $progressBar = new ProgressBar($output, $size); $progressBar->setFormat('%current%/%max% %bar% %percent:3s%%'); $progressBar->setRedrawFrequency(max(1, floor($size / 1000))); $progressBar->setBarWidth(60); if (!defined('PHP_WINDOWS_VERSION_BUILD')) { $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591 $progressBar->setProgressCharacter(''); $progressBar->setBarCharacter('▓'); // dark shade character \u2593 } $progressBar->start(); } $progressBar->setProgress($downloaded); }; $client = new Client(); $client->getEmitter()->attach(new Progress(null, $downloadCallback)); // store the file in a temporary hidden directory with a random name $this->compressedFilePath = \ProcessWire\wire('config')->paths->siteModules . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . $module . '.' . pathinfo($pwArchiveFile, PATHINFO_EXTENSION); try { $response = $client->get($pwArchiveFile); } catch (ClientException $e) { if ($e->getCode() === 403 || $e->getCode() === 404) { throw new \RuntimeException("The selected module {$module} cannot be downloaded because it does not exist.\n"); } else { throw new \RuntimeException(sprintf("The selected module (%s) couldn't be downloaded because of the following error:\n%s", $module, $e->getMessage())); } } $fs = new Filesystem(); $fs->dumpFile($this->compressedFilePath, $response->getBody()); if (null !== $progressBar) { $progressBar->finish(); $output->writeln("\n"); } return $this; }
/** * Extract the zip file into the given directory. * * @param string $zipFile * @param string $directory * @return $this */ protected function extract($zipFile, $directory) { $this->stdout("\nExtract project...\n", Console::FG_GREEN); $distill = new Distill(); $distill->extractWithoutRootDirectory($zipFile, $directory); return $this; }
<?php require __DIR__ . '/../vendor/autoload.php'; use Distill\Distill; $distill = new Distill(); $distill->extract(__DIR__ . '/../tests/files/file_ok.zip', __DIR__ . '/extract');