diff --git a/composer.json b/composer.json index 2ea0ecc..9acec00 100644 --- a/composer.json +++ b/composer.json @@ -21,13 +21,14 @@ "homepage": "https://www.dtapp.net", "require": { "php": ">=7.1.0", - "topthink/framework": "^6.0.0", - "topthink/think-orm": "^2.0", - "liguangchun/ip": "^1.1", "ext-gd": "*", "ext-libxml": "*", "ext-json": "*", - "ext-simplexml": "*" + "ext-simplexml": "*", + "ext-zip": "*", + "topthink/framework": "^6.0.0", + "topthink/think-orm": "^2.0", + "liguangchun/ip": "^1.1" }, "autoload": { "files": [ diff --git a/src/Files.php b/src/Files.php index 4f085a1..d35960e 100644 --- a/src/Files.php +++ b/src/Files.php @@ -16,6 +16,9 @@ namespace DtApp\ThinkLibrary; +use DtApp\ThinkLibrary\exception\DtAppException; +use ZipArchive; + /** * 文件管理类 * Class Files @@ -28,9 +31,11 @@ class Files * 删除文件 * @param string $name 路径 * @return bool + * @throws DtAppException */ public function delete(string $name) { + if (empty($name)) throw new DtAppException('请检查需要删除文件夹的名称'); if (file_exists($name)) if (unlink($name)) return true; return false; } @@ -39,9 +44,11 @@ class Files * 删除文件夹 * @param string $name 路径 * @return bool + * @throws DtAppException */ public function deletes(string $name) { + if (empty($name)) throw new DtAppException('请检查需要删除文件夹的名称'); //先删除目录下的文件: $dh = opendir($name); while ($file = readdir($dh)) { @@ -62,4 +69,32 @@ class Files return false; } } + + /** + * 把文件夹里面的文件打包成zip文件 + * @param string $name 路径 + * @param string $suffix_name 需要打包的后缀名,默认.png + * @param string $file_name 文件名,默认全部名 + * @return bool + * @throws DtAppException + */ + public function folderZip(string $name, string $suffix_name = '.png', string $file_name = '*') + { + if (empty($name)) throw new DtAppException('请检查需要打包的路径名称'); + try { + // 获取目录下所有某个结尾的文件列表 + $list = glob($name . "{$file_name}.{$suffix_name}"); + $fileList = $list; + $zip = new ZipArchive(); + // 打开压缩包 + $zip->open($name, ZipArchive::CREATE); + //向压缩包中添加文件 + foreach ($fileList as $file) $zip->addFile($file, basename($file)); + //关闭压缩包 + $zip->close(); + return true; + } catch (\Exception $e) { + return false; + } + } } diff --git a/src/facade/Files.php b/src/facade/Files.php index c22d8ec..d83ffaf 100644 --- a/src/facade/Files.php +++ b/src/facade/Files.php @@ -27,6 +27,7 @@ use think\Facade; * * @method \DtApp\ThinkLibrary\Files delete(string $name) bool 删除文件 * @method \DtApp\ThinkLibrary\Files deletes(string $name) bool 删除文件夹 + * @method \DtApp\ThinkLibrary\Files folderZip(string $name, string $suffix_name = '.png', string $file_name = '*') bool 把文件夹里面的文件打包成zip文件 */ class Files extends Facade {