This commit is contained in:
47
api/app/Utils/ZipUtil.php
Normal file
47
api/app/Utils/ZipUtil.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use Exception;
|
||||
use ZipArchive;
|
||||
|
||||
class ZipUtil
|
||||
{
|
||||
public static function extract($file, $destination)
|
||||
{
|
||||
$zipper = new ZipArchive();
|
||||
|
||||
if(!$zipper->open($file)) {
|
||||
throw new Exception('Erro ao abrir o arquivo ZIP.');
|
||||
}
|
||||
|
||||
if($zipper->numFiles == 0) {
|
||||
throw new Exception('Arquivo vazio ou não encontrado.');
|
||||
}
|
||||
|
||||
$zipper->extractTo($destination);
|
||||
|
||||
$zipper->close();
|
||||
}
|
||||
|
||||
public static function compress($files, $destination)
|
||||
{
|
||||
$zipper = new ZipArchive();
|
||||
|
||||
$path = dirname($destination);
|
||||
|
||||
if (file_exists($path) === false) {
|
||||
mkdir($path);
|
||||
}
|
||||
|
||||
if ($zipper->open($destination, ZipArchive::CREATE) !== TRUE) {
|
||||
throw new Exception('Erro ao abrir o diretório de destino do arquivo ZIP.');
|
||||
}
|
||||
|
||||
foreach ($files as $file) {
|
||||
$zipper->addFile($file['path'], $file['name']);
|
||||
}
|
||||
|
||||
$zipper->close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user