blessing-skin-server/app/Services/Unzip.php
2020-01-14 10:58:20 +08:00

38 lines
802 B
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use Exception;
use Illuminate\Filesystem\Filesystem;
use ZipArchive;
class Unzip
{
/** @var Filesystem */
protected $filesystem;
/** @var ZipArchive */
protected $zipper;
public function __construct(Filesystem $filesystem, ZipArchive $zipper)
{
$this->filesystem = $filesystem;
$this->zipper = $zipper;
}
public function extract(string $file, string $destination): void
{
$zip = $this->zipper;
$resource = $zip->open($file);
if ($resource === true && $zip->extractTo($destination)) {
$zip->close();
$this->filesystem->delete($file);
} else {
throw new Exception(trans('admin.download.errors.unzip'));
}
}
}