diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..a98975e --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":[],"times":{"ZanySoft\\Zip\\Tests\\ZipTest::testCreateZipFileWithoutAddingFiles":0.123,"ZanySoft\\Zip\\Tests\\ZipTest::testCreateZipWithAddingFiles":0.016,"ZanySoft\\Zip\\Tests\\ZipTest::testSetSkipThrowsExceptionOnInvalidMode":0.011,"ZanySoft\\Zip\\Tests\\ZipTest::testSetValidSkipMode":0.008,"ZanySoft\\Zip\\Tests\\ZipTest::testListFiles":0.01,"ZanySoft\\Zip\\Tests\\ZipTest::testHasFile":0.012,"ZanySoft\\Zip\\Tests\\ZipTest::testHasFileDoesNotExist":0.012,"ZanySoft\\Zip\\Tests\\ZipTest::testExtract":0.013}} \ No newline at end of file diff --git a/composer.json b/composer.json index c0b505b..f01e613 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,9 @@ "keywords": [ "laravel-zip", "laravel", - "laravel5", + "laravel 6", + "laravel 7", + "laravel 8", "zip", "unzip", "extract", @@ -24,13 +26,24 @@ ], "require": { "php": ">=7.1", - "illuminate/support": "^6.0|^7.0|^8.0" + "illuminate/support": "^6.0|^7.0|^8.0", + "ext-zip": "*" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "scrutinizer/ocular": "^1.9", + "orchestra/testbench": "^6.24" }, "autoload": { "psr-4": { "ZanySoft\\Zip\\": "src" } }, + "autoload-dev": { + "psr-4": { + "ZanySoft\\Zip\\": "tests" + } + }, "extra": { "laravel": { "providers": [ diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..655d24e --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,16 @@ + + + + + ./tests/ + + + \ No newline at end of file diff --git a/src/Zip.php b/src/Zip.php index 6c3deb8..fb7c3ee 100644 --- a/src/Zip.php +++ b/src/Zip.php @@ -1,7 +1,7 @@ 'No error', - ZipArchive::ER_MULTIDISK => 'Multi-disk zip archives not supported', - ZipArchive::ER_RENAME => 'Renaming temporary file failed', - ZipArchive::ER_CLOSE => 'Closing zip archive failed', - ZipArchive::ER_SEEK => 'Seek error', - ZipArchive::ER_READ => 'Read error', - ZipArchive::ER_WRITE => 'Write error', - ZipArchive::ER_CRC => 'CRC error', - ZipArchive::ER_ZIPCLOSED => 'Containing zip archive was closed', - ZipArchive::ER_NOENT => 'No such file', - ZipArchive::ER_EXISTS => 'File already exists', - ZipArchive::ER_OPEN => 'Can\'t open file', - ZipArchive::ER_TMPOPEN => 'Failure to create temporary file', - ZipArchive::ER_ZLIB => 'Zlib error', - ZipArchive::ER_MEMORY => 'Malloc failure', - ZipArchive::ER_CHANGED => 'Entry has been changed', + private static $zip_status_codes = [ + ZipArchive::ER_OK => 'No error', + ZipArchive::ER_MULTIDISK => 'Multi-disk zip archives not supported', + ZipArchive::ER_RENAME => 'Renaming temporary file failed', + ZipArchive::ER_CLOSE => 'Closing zip archive failed', + ZipArchive::ER_SEEK => 'Seek error', + ZipArchive::ER_READ => 'Read error', + ZipArchive::ER_WRITE => 'Write error', + ZipArchive::ER_CRC => 'CRC error', + ZipArchive::ER_ZIPCLOSED => 'Containing zip archive was closed', + ZipArchive::ER_NOENT => 'No such file', + ZipArchive::ER_EXISTS => 'File already exists', + ZipArchive::ER_OPEN => 'Can\'t open file', + ZipArchive::ER_TMPOPEN => 'Failure to create temporary file', + ZipArchive::ER_ZLIB => 'Zlib error', + ZipArchive::ER_MEMORY => 'Malloc failure', + ZipArchive::ER_CHANGED => 'Entry has been changed', ZipArchive::ER_COMPNOTSUPP => 'Compression method not supported', - ZipArchive::ER_EOF => 'Premature EOF', - ZipArchive::ER_INVAL => 'Invalid argument', - ZipArchive::ER_NOZIP => 'Not a zip archive', - ZipArchive::ER_INTERNAL => 'Internal error', - ZipArchive::ER_INCONS => 'Zip archive inconsistent', - ZipArchive::ER_REMOVE => 'Can\'t remove file', - ZipArchive::ER_DELETED => 'Entry has been deleted' - ); + ZipArchive::ER_EOF => 'Premature EOF', + ZipArchive::ER_INVAL => 'Invalid argument', + ZipArchive::ER_NOZIP => 'Not a zip archive', + ZipArchive::ER_INTERNAL => 'Internal error', + ZipArchive::ER_INCONS => 'Zip archive inconsistent', + ZipArchive::ER_REMOVE => 'Can\'t remove file', + ZipArchive::ER_DELETED => 'Entry has been deleted' + ]; /** * Class constructor * - * @param string $zip_file ZIP file name + * @param string $zip_file ZIP file name * */ - public function __construct($zip_file) { - + public function __construct($zip_file) + { if (empty($zip_file)) { throw new \Exception(self::getStatus(ZipArchive::ER_NOENT)); } $this->zip_file = $zip_file; - } /** * Open a zip archive * - * @param string $zip_file ZIP file name + * @param string $zip_file ZIP file name * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - public static function open($zip_file) { - + public static function open($zip_file) + { try { - $zip = new Zip($zip_file); $zip->setArchive(self::openZipFile($zip_file)); - } catch (\Exception $ze) { - throw $ze; - } return $zip; - } /** * Check a zip archive * - * @param string $zip_file ZIP file name + * @param string $zip_file ZIP file name * * @return bool */ - public static function check($zip_file) { - + public static function check($zip_file) + { try { - $zip = self::openZipFile($zip_file, ZipArchive::CHECKCONS); $zip->close(); - } catch (Exception $ze) { - throw $ze; - } return true; - } /** * Create a new zip archive * - * @param string $zip_file ZIP file name - * @param bool $overwrite overwrite existing file (if any) + * @param string $zip_file ZIP file name + * @param bool $overwrite overwrite existing file (if any) * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - public static function create($zip_file, $overwrite = false) { - - $overwrite = filter_var($overwrite, FILTER_VALIDATE_BOOLEAN, array( - "options" => array( - "default" => false - ) - )); + public static function create($zip_file, $overwrite = false) + { + $overwrite = filter_var($overwrite, FILTER_VALIDATE_BOOLEAN, [ + 'options' => [ + 'default' => false + ] + ]); try { - $zip = new Zip($zip_file); if ($overwrite) { @@ -187,36 +176,31 @@ public static function create($zip_file, $overwrite = false) { } else { $zip->setArchive(self::openZipFile($zip_file, ZipArchive::CREATE)); } - } catch (Exception $ze) { - throw $ze; - } return $zip; - } /** * Set files to skip * - * @param string $mode [HIDDEN, ZANYSOFT, ALL, NONE] + * @param string $mode [HIDDEN, ZANYSOFT, ALL, NONE] * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - final public function setSkipped($mode) { - + final public function setSkipped($mode) + { $mode = strtoupper($mode); if (!in_array($mode, $this->supported_skip_modes)) { - throw new Exception("Unsupported skip mode"); + throw new Exception('Unsupported skip mode'); } $this->skip_mode = $mode; return $this; - } /** @@ -224,25 +208,23 @@ final public function setSkipped($mode) { * * @return string */ - final public function getSkipped() { - + final public function getSkipped() + { return $this->skip_mode; - } /** * Set extraction password * - * @param string $password + * @param string $password * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - final public function setPassword($password) { - + final public function setPassword($password) + { $this->password = $password; return $this; - } /** @@ -250,29 +232,27 @@ final public function setPassword($password) { * * @return string */ - final public function getPassword() { - + final public function getPassword() + { return $this->password; - } /** * Set current base path (just to add relative files to zip archive) * - * @param string $path + * @param string $path * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - final public function setPath($path) { - + final public function setPath($path) + { if (!file_exists($path)) { - throw new Exception("Not existent path"); + throw new Exception('Not existent path'); } - $this->path = $path[strlen($path) - 1] == "/" ? $path : $path . "/"; + $this->path = $path[strlen($path) - 1] == '/' ? $path : $path . '/'; return $this; - } /** @@ -280,32 +260,30 @@ final public function setPath($path) { * * @return string */ - final public function getPath() { - + final public function getPath() + { return $this->path; - } /** * Set extraction folder mask * - * @param int $mask + * @param int $mask * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - final public function setMask($mask) { - - $mask = filter_var($mask, FILTER_VALIDATE_INT, array( - "options" => array( - "max_range" => 0777, - "default" => 0777 - ), 'flags' => FILTER_FLAG_ALLOW_OCTAL - )); + final public function setMask($mask) + { + $mask = filter_var($mask, FILTER_VALIDATE_INT, [ + 'options' => [ + 'max_range' => 0777, + 'default' => 0777 + ], 'flags' => FILTER_FLAG_ALLOW_OCTAL + ]); $this->mask = $mask; return $this; - } /** @@ -313,25 +291,23 @@ final public function setMask($mask) { * * @return int */ - final public function getMask() { - + final public function getMask() + { return $this->mask; - } /** * Set the current ZipArchive object * - * @param \ZipArchive $zip + * @param \ZipArchive $zip * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - final public function setArchive(ZipArchive $zip) { - + final public function setArchive(ZipArchive $zip) + { $this->zip_archive = $zip; return $this; - } /** @@ -339,10 +315,9 @@ final public function setArchive(ZipArchive $zip) { * * @return \ZipArchive */ - final public function getArchive() { - + final public function getArchive() + { return $this->zip_archive; - } /** @@ -350,17 +325,17 @@ final public function getArchive() { * * @return string */ - final public function getZipFile() { - + final public function getZipFile() + { return $this->zip_file; - } /** * Get an SplFileObject for the zip file - * @return SplFileObject + * @return \SplFileObject */ - public function getFileObject() { + public function getFileObject() + { return new \SplFileObject($this->zip_file); } @@ -369,12 +344,11 @@ public function getFileObject() { * * @return array */ - public function listFiles() { - - $list = Array(); + public function listFiles() + { + $list = []; for ($i = 0; $i < $this->zip_archive->numFiles; $i++) { - $name = $this->zip_archive->getNameIndex($i); if ($name === false) { @@ -382,22 +356,21 @@ public function listFiles() { } array_push($list, $name); - } return $list; - } /** * Check if zip archive has a file * - * @param string $file File - * @param int $flags (optional) ZipArchive::FL_NOCASE, ZipArchive::FL_NODIR seperated by bitwise OR + * @param string $file File + * @param int $flags (optional) ZipArchive::FL_NOCASE, ZipArchive::FL_NODIR seperated by bitwise OR * * @return bool */ - public function has($file, $flags = 0) { + public function has($file, $flags = 0) + { if (empty($file)) { throw new Exception('Invalid File'); } @@ -408,19 +381,18 @@ public function has($file, $flags = 0) { /** * Extract files from zip archive * - * @param string $destination Destination path - * @param mixed $files (optional) a filename or an array of filenames + * @param string $destination Destination path + * @param mixed $files (optional) a filename or an array of filenames * * @return bool */ - public function extract($destination, $files = null) { - + public function extract($destination, $files = null) + { if (empty($destination)) { throw new Exception('Invalid destination path'); } if (!file_exists($destination)) { - $omask = umask(0); $action = mkdir($destination, $this->mask, true); @@ -428,23 +400,18 @@ public function extract($destination, $files = null) { umask($omask); if ($action === false) { - throw new Exception("Error creating folder " . $destination); + throw new Exception('Error creating folder ' . $destination); } - } if (!is_writable($destination)) { throw new Exception('Destination path not writable'); } - if (is_array($files) && @sizeof($files) != 0) { - + if (is_array($files) && count($files) != 0) { $file_matrix = $files; - } else { - $file_matrix = $this->getArchiveFiles(); - } if (!empty($this->password)) { @@ -458,84 +425,69 @@ public function extract($destination, $files = null) { } return true; - } /** * Add files to zip archive * - * @param mixed $file_name_or_array filename to add or an array of filenames - * @param bool $flatten_root_folder in case of directory, specify if root folder should be flatten or not + * @param mixed $file_name_or_array filename to add or an array of filenames + * @param bool $flatten_root_folder in case of directory, specify if root folder should be flatten or not * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - public function add($file_name_or_array, $flatten_root_folder = false) { - + public function add($file_name_or_array, $flatten_root_folder = false) + { if (empty($file_name_or_array)) { throw new Exception(self::getStatus(ZipArchive::ER_NOENT)); } - $flatten_root_folder = filter_var($flatten_root_folder, FILTER_VALIDATE_BOOLEAN, array( - "options" => array( - "default" => false - ) - )); + $flatten_root_folder = filter_var($flatten_root_folder, FILTER_VALIDATE_BOOLEAN, [ + 'options' => [ + 'default' => false + ] + ]); try { - if (is_array($file_name_or_array)) { - foreach ($file_name_or_array as $file_name) { $this->addItem($file_name, $flatten_root_folder); } - } else { $this->addItem($file_name_or_array, $flatten_root_folder); } - } catch (Exception $ze) { - throw $ze; - } return $this; - } /** * Delete files from zip archive * - * @param mixed $file_name_or_array filename to delete or an array of filenames + * @param mixed $file_name_or_array filename to delete or an array of filenames * - * @return \ZanySoft\Zip\Zip + * @return Zip */ - public function delete($file_name_or_array) { - + public function delete($file_name_or_array) + { if (empty($file_name_or_array)) { throw new Exception(self::getStatus(ZipArchive::ER_NOENT)); } try { - if (is_array($file_name_or_array)) { - foreach ($file_name_or_array as $file_name) { $this->deleteItem($file_name); } - } else { $this->deleteItem($file_name_or_array); } - } catch (Exception $ze) { - throw $ze; - } return $this; - } /** @@ -543,14 +495,13 @@ public function delete($file_name_or_array) { * * @return bool */ - public function close() { - + public function close() + { if ($this->zip_archive->close() === false) { throw new Exception(self::getStatus($this->zip_archive->status)); } return true; - } /** @@ -558,12 +509,11 @@ public function close() { * * @return array */ - private function getArchiveFiles() { - - $list = array(); + private function getArchiveFiles() + { + $list = []; for ($i = 0; $i < $this->zip_archive->numFiles; $i++) { - $file = $this->zip_archive->statIndex($i); if ($file === false) { @@ -572,32 +522,31 @@ private function getArchiveFiles() { $name = str_replace('\\', '/', $file['name']); - if ($name[0] == "." AND in_array($this->skip_mode, array("HIDDEN", "ALL"))) { + if ($name[0] == '.' and in_array($this->skip_mode, ['HIDDEN', 'ALL'])) { continue; } - if ($name[0] == "." AND @$name[1] == "_" AND in_array($this->skip_mode, array("ZANYSOFT", "ALL"))) { + if ($name[0] == '.' and @$name[1] == '_' and in_array($this->skip_mode, ['ZANYSOFT', 'ALL'])) { continue; } array_push($list, $name); - } return $list; - } /** * Add item to zip archive * - * @param string $file File to add (realpath) - * @param bool $flatroot (optional) If true, source directory will be not included - * @param string $base (optional) Base to record in zip file + * @param string $file File to add (realpath) + * @param bool $flatroot (optional) If true, source directory will be not included + * @param string|null $base (optional) Base to record in zip file * + * @throws Exception */ - private function addItem($file, $flatroot = false, $base = null) { - + private function addItem($file, $flatroot = false, $base = null) + { $file = is_null($this->path) ? $file : $this->path . $file; $real_file = str_replace('\\', '/', realpath($file)); @@ -605,21 +554,17 @@ private function addItem($file, $flatroot = false, $base = null) { $real_name = basename($real_file); if (!is_null($base)) { - - if ($real_name[0] == "." AND in_array($this->skip_mode, array("HIDDEN", "ALL"))) { + if ($real_name[0] == '.' and in_array($this->skip_mode, ['HIDDEN', 'ALL'])) { return; } - if ($real_name[0] == "." AND @$real_name[1] == "_" AND in_array($this->skip_mode, array("ZANYSOFT", "ALL"))) { + if ($real_name[0] == '.' and @$real_name[1] == '_' and in_array($this->skip_mode, ['ZANYSOFT', 'ALL'])) { return; } - } if (is_dir($real_file)) { - if (!$flatroot) { - $folder_target = is_null($base) ? $real_name : $base . $real_name; $new_folder = $this->zip_archive->addEmptyDir($folder_target); @@ -627,37 +572,26 @@ private function addItem($file, $flatroot = false, $base = null) { if ($new_folder === false) { throw new Exception(self::getStatus($this->zip_archive->status)); } - } else { - $folder_target = null; - } foreach (new \DirectoryIterator($real_file) as $path) { - if ($path->isDot()) { continue; } $file_real = $path->getPathname(); - $base = is_null($folder_target) ? null : ($folder_target . "/"); + $base = is_null($folder_target) ? null : ($folder_target . '/'); try { - $this->addItem($file_real, false, $base); - } catch (Exception $ze) { - throw $ze; - } - } - } else if (is_file($real_file)) { - $file_target = is_null($base) ? $real_name : $base . $real_name; $add_file = $this->zip_archive->addFile($real_file, $file_target); @@ -665,39 +599,36 @@ private function addItem($file, $flatroot = false, $base = null) { if ($add_file === false) { throw new Exception(self::getStatus($this->zip_archive->status)); } - } else { return; } - } /** * Delete item from zip archive * - * @param string $file File to delete (zippath) + * @param string $file File to delete (zippath) * */ - private function deleteItem($file) { - + private function deleteItem($file) + { $deleted = $this->zip_archive->deleteName($file); if ($deleted === false) { throw new \Exception(self::getStatus($this->zip_archive->status)); } - } /** * Open a zip file * - * @param string $zip_file ZIP status code - * @param int $flags ZIP status code + * @param string $zip_file ZIP status code + * @param int $flags ZIP status code * * @return \ZipArchive */ - private static function openZipFile($zip_file, $flags = null) { - + private static function openZipFile($zip_file, $flags = null) + { $zip = new ZipArchive(); $open = $zip->open($zip_file, $flags); @@ -707,23 +638,21 @@ private static function openZipFile($zip_file, $flags = null) { } return $zip; - } /** * Get status from zip status code * - * @param int $code ZIP status code + * @param int $code ZIP status code * * @return string */ - private static function getStatus($code) { - + private static function getStatus($code) + { if (array_key_exists($code, self::$zip_status_codes)) { return self::$zip_status_codes[$code]; } else { return sprintf('Unknown status %s', $code); } - } } diff --git a/src/ZipFacade.php b/src/ZipFacade.php index e856df3..e3f1763 100644 --- a/src/ZipFacade.php +++ b/src/ZipFacade.php @@ -1,4 +1,5 @@ zip_archives[] = $zip; return $this; - } /** * Remove a \Coodojo\Zip\Zip object from manager * - * @param \ZanySoft\Zip\Zip $zip + * @param \ZanySoft\Zip\Zip $zip * * @return \ZanySoft\Zip\ZipManager */ - public function removeZip(\ZanySoft\Zip\Zip $zip) { - + public function removeZip(\ZanySoft\Zip\Zip $zip) + { $archive_key = array_search($zip, $this->zip_archives, true); - if ( $archive_key === false ) throw new Exception("Archive not found"); + if ($archive_key === false) { + throw new Exception('Archive not found'); + } unset($this->zip_archives[$archive_key]); return $this; - } /** @@ -60,53 +61,52 @@ public function removeZip(\ZanySoft\Zip\Zip $zip) { * * @return array */ - public function listZips() { - - $list = array(); + public function listZips() + { + $list = []; - foreach ( $this->zip_archives as $key=>$archive ) $list[$key] = $archive->getZipFile(); + foreach ($this->zip_archives as $key => $archive) { + $list[$key] = $archive->getZipFile(); + } return $list; - } /** * Get a a \Coodojo\Zip\Zip object * - * @param int $zipId The zip id from self::listZips() + * @param int $zipId The zip id from self::listZips() * * @return \ZanySoft\Zip\Zip */ - public function getZip($zipId) { - - if ( array_key_exists($zipId, $this->zip_archives) === false ) throw new Exception("Archive not found"); + public function getZip($zipId) + { + if (array_key_exists($zipId, $this->zip_archives) === false) { + throw new Exception('Archive not found'); + } return $this->zip_archives[$zipId]; - } /** * Set current base path (just to add relative files to zip archive) * for all zip files * - * @param string $path + * @param string $path * * @return \ZanySoft\Zip\ZipManager */ - public function setPath($path) { - + public function setPath($path) + { try { - - foreach ( $this->zip_archives as $archive ) $archive->setPath($path); - + foreach ($this->zip_archives as $archive) { + $archive->setPath($path); + } } catch (Exception $ze) { - throw $ze; - } return $this; - } /** @@ -114,37 +114,35 @@ public function setPath($path) { * * @return array */ - public function getPath() { - - $paths = array(); + public function getPath() + { + $paths = []; - foreach ( $this->zip_archives as $key=>$archive ) $paths[$key] = $archive->getPath(); + foreach ($this->zip_archives as $key => $archive) { + $paths[$key] = $archive->getPath(); + } return $paths; - } /** * Set default file mask for all Zips * - * @param int $mask + * @param int $mask * * @return \ZanySoft\Zip\ZipManager */ - public function setMask($mask) { - + public function setMask($mask) + { try { - - foreach ( $this->zip_archives as $archive ) $archive->setMask($mask); - + foreach ($this->zip_archives as $archive) { + $archive->setMask($mask); + } } catch (Exception $ze) { - throw $ze; - } return $this; - } /** @@ -152,14 +150,15 @@ public function setMask($mask) { * * @return array */ - public function getMask() { + public function getMask() + { + $masks = []; - $masks = array(); - - foreach ( $this->zip_archives as $key=>$archive ) $masks[$key] = $archive->getMask(); + foreach ($this->zip_archives as $key => $archive) { + $masks[$key] = $archive->getMask(); + } return $masks; - } /** @@ -167,75 +166,64 @@ public function getMask() { * * @return array */ - public function listFiles() { - - $files = array(); + public function listFiles() + { + $files = []; try { - - foreach ( $this->zip_archives as $key=>$archive ) $files[$key] = $archive->listFiles(); - + foreach ($this->zip_archives as $key => $archive) { + $files[$key] = $archive->listFiles(); + } } catch (Exception $ze) { - throw $ze; - } return $files; - } /** * Extract Zips to common destination * - * @param string $destination Destination path - * @param bool $separate Specify if files should be placed in different directories - * @param array $files Array of files to extract + * @param string $destination Destination path + * @param bool $separate Specify if files should be placed in different directories + * @param array $files Array of files to extract * * @return bool */ - public function extract($destination, $separate = true, $files = null) { - + public function extract($destination, $separate = true, $files = null) + { try { - - foreach ( $this->zip_archives as $archive ) { - - $local_path = substr($destination, -1) == '/' ? $destination : $destination.'/'; + foreach ($this->zip_archives as $archive) { + $local_path = substr($destination, -1) == '/' ? $destination : $destination . '/'; $local_file = pathinfo($archive->getZipFile()); - $local_destination = $separate ? ($local_path.$local_file['filename']) : $destination; + $local_destination = $separate ? ($local_path . $local_file['filename']) : $destination; $archive->extract($local_destination, $files = null); - } - } catch (Exception $ze) { - throw $ze; - } return true; - } /** * Merge multiple Zips into one * - * @param string $output_zip_file Destination zip - * @param bool $separate Specify if files should be placed in different directories + * @param string $output_zip_file Destination zip + * @param bool $separate Specify if files should be placed in different directories * * @return bool */ - public function merge($output_zip_file, $separate = true) { - + public function merge($output_zip_file, $separate = true) + { $pathinfo = pathinfo($output_zip_file); - $temporary_folder = $pathinfo['dirname']."/".self::getTemporaryFolder(); + $temporary_folder = $pathinfo['dirname'] . '/' . self::getTemporaryFolder(); try { - $this->extract($temporary_folder, $separate, null); $zip = Zip::create($output_zip_file); @@ -243,66 +231,52 @@ public function merge($output_zip_file, $separate = true) { $zip->add($temporary_folder, true)->close(); self::recursiveUnlink($temporary_folder); - - } catch (Exception $ze) { - - throw $ze; - } catch (Exception $e) { - throw $e; - } return true; - } /** * Add a file to zip * - * @param mixed $file_name_or_array filename to add or an array of filenames - * @param bool $flatten_root_folder in case of directory, specify if root folder should be flatten or not + * @param mixed $file_name_or_array filename to add or an array of filenames + * @param bool $flatten_root_folder in case of directory, specify if root folder should be flatten or not * * @return \ZanySoft\Zip\ZipManager */ - public function add($file_name_or_array, $flatten_root_folder = false) { - + public function add($file_name_or_array, $flatten_root_folder = false) + { try { - - foreach ( $this->zip_archives as $archive ) $archive->add($file_name_or_array, $flatten_root_folder); - + foreach ($this->zip_archives as $archive) { + $archive->add($file_name_or_array, $flatten_root_folder); + } } catch (Exception $ze) { - throw $ze; - } return $this; - } /** * Delete a file from Zips * - * @param mixed $file_name_or_array filename to add or an array of filenames + * @param mixed $file_name_or_array filename to add or an array of filenames * * @return \ZanySoft\Zip\ZipManager */ - public function delete($file_name_or_array) { - + public function delete($file_name_or_array) + { try { - - foreach ( $this->zip_archives as $archive ) $archive->delete($file_name_or_array); - + foreach ($this->zip_archives as $archive) { + $archive->delete($file_name_or_array); + } } catch (Exception $ze) { - throw $ze; - } return $this; - } /** @@ -310,63 +284,58 @@ public function delete($file_name_or_array) { * * @return bool */ - public function close() { - + public function close() + { try { - - foreach ( $this->zip_archives as $archive ) $archive->close(); - + foreach ($this->zip_archives as $archive) { + $archive->close(); + } } catch (Exception $ze) { - throw $ze; - } return true; - } - private static function removeExtension($filename) { - + /** + * @param $filename + * @return mixed|string + */ + private static function removeExtension($filename) + { $file_info = pathinfo($filename); return $file_info['filename']; - } - private static function getTemporaryFolder() { - - return "zip-temp-folder-".md5(uniqid(rand(), true), 0); - + private static function getTemporaryFolder() + { + return 'zip-temp-folder-' . md5(uniqid((string)rand(), true), false); } /** * @param string $folder */ - private static function recursiveUnlink($folder) { - - foreach ( new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folder, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path ) { - + private static function recursiveUnlink($folder) + { + foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folder, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $path) { $pathname = $path->getPathname(); - if ( $path->isDir() ) { - + if ($path->isDir()) { $action = rmdir($pathname); - } else { - $action = unlink($pathname); - } - if ( $action === false ) throw new Exception("Error deleting ".$pathname." during recursive unlink of folder ".$folder); - + if ($action === false) { + throw new Exception('Error deleting ' . $pathname . ' during recursive unlink of folder ' . $folder); + } } $action = rmdir($folder); - if ( $action === false ) throw new Exception("Error deleting folder ".$folder); - + if ($action === false) { + throw new Exception('Error deleting folder ' . $folder); + } } - } diff --git a/src/ZipServiceProvider.php b/src/ZipServiceProvider.php index 9ea8d14..3a7da02 100644 --- a/src/ZipServiceProvider.php +++ b/src/ZipServiceProvider.php @@ -4,15 +4,15 @@ use Illuminate\Support\ServiceProvider; -class ZipServiceProvider extends ServiceProvider { - - +class ZipServiceProvider extends ServiceProvider +{ /** * Register the service provider. * * @return void */ - public function register() { + public function register() + { $this->registerCpanelService(); /*if ($this->app->runningInConsole()) { @@ -25,7 +25,8 @@ public function register() { * * @return void */ - public function registerCpanelService() { + public function registerCpanelService() + { $this->app->singleton('zip', function ($app) { return new Zip($app); }); @@ -54,7 +55,4 @@ protected function isLumen() { return str_contains($this->app->version(), 'Lumen') === true; } - -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/tests/TestFiles/file1.txt b/tests/TestFiles/file1.txt new file mode 100644 index 0000000..4aa8a17 --- /dev/null +++ b/tests/TestFiles/file1.txt @@ -0,0 +1 @@ +Test File 1 Content \ No newline at end of file diff --git a/tests/TestFiles/file2.txt b/tests/TestFiles/file2.txt new file mode 100644 index 0000000..bd883c2 --- /dev/null +++ b/tests/TestFiles/file2.txt @@ -0,0 +1 @@ +Test File 2 Content \ No newline at end of file diff --git a/tests/TestFiles/password.zip b/tests/TestFiles/password.zip new file mode 100644 index 0000000..0e2853a Binary files /dev/null and b/tests/TestFiles/password.zip differ diff --git a/tests/ZipTest.php b/tests/ZipTest.php new file mode 100644 index 0000000..925501e --- /dev/null +++ b/tests/ZipTest.php @@ -0,0 +1,128 @@ +testFilePath = __DIR__ . '/TestFiles'; + } + + public function tearDown(): void + { + parent::tearDown(); + +// array_map('unlink', glob($this->filesPath('Zips/*')) ?: []); + $this->deleteTestFiles(); + } + + public function testCreateZipFileWithoutAddingFiles() + { + $zip = Zip::create($this->filesPath('Zips/TestZip.zip'), true); + $zip->close(); + + $this->assertFalse(file_exists($this->filesPath('Zips/TestZip.zip'))); + } + + public function testCreateZipWithAddingFiles() + { + $zip = Zip::create($this->filesPath('Zips/TestZip.zip')); + $zip->add($this->filesPath('file1.txt')); + $zip->close(); + + $this->assertTrue(file_exists($this->filesPath('Zips/TestZip.zip'))); + } + + public function testSetSkipThrowsExceptionOnInvalidMode() + { + $this->expectException(\Exception::class); + + $zip = Zip::create($this->filesPath('Zips/TestZip.zip')); + $zip->setSkipped('doesntexist'); + } + + public function testSetValidSkipMode() + { + $zip = Zip::create($this->filesPath('Zips/TestZip.zip')); + $zip->setSkipped('hidden'); + $zip->close(); + + $this->assertEquals('HIDDEN', $zip->getSkipped()); + } + + public function testListFiles() + { + $zip = Zip::create($this->filesPath('Zips/TestZip.zip')); + $zip->add($this->filesPath('file1.txt')); + $zip->add($this->filesPath('file2.txt')); + + $this->assertEquals(2, count($zip->listFiles())); + $this->assertEquals('file1.txt', $zip->listFiles()[0]); + $this->assertEquals('file2.txt', $zip->listFiles()[1]); + + $zip->close(); + } + + public function testHasFile() + { + $zip = Zip::create($this->filesPath('Zips/TestZip.zip')); + $zip->add($this->filesPath('file1.txt')); + + $this->assertTrue($zip->has('file1.txt')); + + $zip->close(); + } + + public function testHasFileDoesNotExist() + { + $zip = Zip::create($this->filesPath('Zips/TestZip.zip')); + $zip->add($this->filesPath('file1.txt')); + + $this->assertFalse($zip->has('file2.txt')); + + $zip->close(); + } + + public function testExtract() + { + $zip = Zip::open($this->filesPath('password.zip')); + $zip->setPassword('password'); + $zip->extract($this->filesPath('Zips')); + + $this->assertTrue(file_exists($this->filesPath('Zips/file1.txt'))); + + $zip->close(); + } + + public function deleteTestFiles() + { + $directoryPath = $this->filesPath('Zips'); + + $files = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($directoryPath, \RecursiveDirectoryIterator::SKIP_DOTS), + \RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach ($files as $fileinfo) { + $removeFunction = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); + $removeFunction($fileinfo->getRealPath()); + } + + return true; + } + + private function filesPath($file) + { + return $this->testFilePath . '/' . $file; + } +} \ No newline at end of file