/** * @param string $sourcePath a dir will compress * @param string $archiveFile zip file save path * @param bool $override * @return bool * @throws FileSystemException * @throws NotFoundException * @throws \inhere\librarys\exceptions\IOException * @throws \inhere\librarys\exceptions\InvalidArgumentException */ public function encode($sourcePath, $archiveFile, $override = true) { if (!class_exists('ZipArchive')) { throw new NotFoundException('The method is require class ZipArchive (by zip extension)'); } // 是一些指定文件 if (is_array($sourcePath)) { $files = new \ArrayObject(); } else { $files = $this->finder->findAll(true, $sourcePath)->getFiles(); } // no file if (!$files->count()) { return false; } $archiveFile = FileSystem::isAbsPath($archiveFile) ? $archiveFile : dirname($sourcePath) . '/' . $archiveFile; FileSystem::mkdir(dirname($archiveFile)); try { $za = $this->getDriver(); if (true !== $za->open($archiveFile, $override ? ZipArchive::OVERWRITE : ZipArchive::CREATE)) { return false; } $za->setArchiveComment('compressed at ' . date('Y-m-d H:i:s')); foreach ($files as $file) { $file = FileSystem::isAbsPath($file) ? $file : $this->sourcePath . '/' . $file; $za->addFile($file); // $za->addFile($this->sourcePath . $path, $path); } return $za->close(); } catch (\Exception $e) { throw new FileSystemException("Compress directory [{$sourcePath}] to archive file [{$archiveFile}] failure!! MSG:" . $e->getMessage()); } }
/** * @param string $sourcePath a dir will compress * @param string $archiveFile zip file save path * @param bool $override * @return bool * @throws FileSystemException * @throws NotFoundException * @throws \inhere\librarys\exceptions\IOException * @throws \inhere\librarys\exceptions\InvalidArgumentException */ public function encode($sourcePath, $archiveFile, $override = true) { if (!class_exists('ZipArchive')) { throw new NotFoundException('The method is require class ZipArchive (by zip extension)'); } // 是一些指定文件 if (is_array($sourcePath)) { $files = new \ArrayObject($sourcePath); } else { $files = $this->finder->findAll(true, $sourcePath)->getFiles(); } // no file if (!$files->count()) { return false; } $archiveFile = FileSystem::isAbsPath($archiveFile) ? $archiveFile : dirname($sourcePath) . '/' . $archiveFile; $archiveFileDir = dirname($archiveFile); FileSystem::mkdir($archiveFileDir); try { $pd = $this->driver = new PharData($archiveFileDir . '/temp-archive.tar'); // ADD FILES TO archive.tar FILE foreach ($files as $file) { $file = FileSystem::isAbsPath($file) ? $file : $this->sourcePath . '/' . $file; $pd->addFile($file); // $pd->addFile($this->sourcePath . $path, $path); } // COMPRESS archive.tar FILE. COMPRESSED FILE WILL BE archive.tar.gz $pd->compress(Phar::GZ); // NOTE THAT BOTH FILES WILL EXISTS. SO IF YOU WANT YOU CAN UNLINK archive.tar return unlink('archive.tar'); } catch (\Exception $e) { throw new FileSystemException("Compress directory [{$sourcePath}] to archive file [{$archiveFile}] failure!! MSG:" . $e->getMessage()); } }
/** * @param $file * @param string $fileKey * @throws InvalidArgumentException * @throws NotFoundException */ public function addLangFile($file, $fileKey = '') { if (!FileSystem::isAbsPath($file)) { $file = $this->buildLangFilePath($file); } if (!is_file($file)) { throw new NotFoundException("The language file don't exists. FILE: {$file}"); } $fileKey = $fileKey ?: basename($file, '.' . $this->fileType); if (!preg_match('/^[a-z][\\w-]+$/i', $fileKey)) { throw new InvalidArgumentException("language file key [{$fileKey}] naming format error!!"); } if ($this->hasLangFile($fileKey)) { throw new InvalidArgumentException("language file key [{$fileKey}] have been exists, don't allow override!!"); } $this->langFiles[trim($fileKey)] = $file; }