public function testCreateZipArchive() { $zip = new ZipArchive64(); $zip->open('Tests/res/test.zip', ZipArchive::OVERWRITE); foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('Tests/res/example')) as $filename) { if (true === $filename->isDir()) { continue; } $zip->addFile($filename); } $zip->close(); $zip = new ZipArchive64(); $zip->open('Tests/res/test.zip'); $zip->extractTo('Tests/res/result'); $zip->close(); }
/** * @param InputInterface $input * @param OutputInterface $output * * @return int|null|void */ protected function execute(InputInterface $input, OutputInterface $output) { $source = $input->getArgument('source'); if ('/' !== $source[0]) { $source = getcwd() . '/' . $source; } if (false === is_dir($source)) { $output->writeln(sprintf('Cannot create from <%s>', $source)); exit; } $target = $input->getArgument('target'); if (true === empty($target)) { $target = getcwd(); } elseif ('/' !== $target[0]) { $target = getcwd() . '/' . $target; } if (false === is_dir(dirname($target))) { $output->writeln(sprintf('Cannot create archive to <%s>', dirname($target))); exit; } $zip = new ZipArchive64(); $update = $input->getOption('update'); if (false === $zip->open($target, $this->getArchiveMode($update))) { $output->writeln(sprintf('Cannot open <%s>', $source)); exit; } $recursive = $input->getOption('recursive'); $directory = $this->getDirectoryIterator($source, false === empty($recursive)); $i = 0; foreach ($directory as $fileInfo) { if (true === $fileInfo->isDir()) { continue; } $i++; $file = $fileInfo->getRealPath(); $truncate = $input->getOption('truncate'); $localName = $this->getLocalName($file, false === empty($truncate)); $verbose = $input->getOption('verbose'); if (false === empty($verbose)) { $output->writeln(sprintf('<%s> Add "%s"', str_pad($i, 4, '0', STR_PAD_LEFT), $localName)); } if (false === $zip->addFile($file, $localName)) { $output->writeln(sprintf('Cannot add source file <%s>', $file)); exit; } } $zip->close(); }