48 lines
1.0 KiB
PHP
48 lines
1.0 KiB
PHP
<?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();
|
|
}
|
|
}
|