Архивирование папки в Zip на PHP
Для архивирования папки можно воспользоваться такой функцией:
public static function zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', DIRECTORY_SEPARATOR, realpath($source));
$source = str_replace('/', DIRECTORY_SEPARATOR, $source);
if (is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = str_replace('\\', DIRECTORY_SEPARATOR, $file);
$file = str_replace('/', DIRECTORY_SEPARATOR, $file);
if ($file == '.' || $file == '..' || empty($file) || $file == DIRECTORY_SEPARATOR) {
continue;
}
// Ignore "." and ".." folders
if (in_array(substr($file, strrpos($file, DIRECTORY_SEPARATOR) + 1), array('.', '..'))) {
continue;
}
$file = realpath($file);
$file = str_replace('\\', DIRECTORY_SEPARATOR, $file);
$file = str_replace('/', DIRECTORY_SEPARATOR, $file);
if (is_dir($file) === true) {
$d = str_replace($source . DIRECTORY_SEPARATOR, '', $file);
if (empty($d)) {
continue;
}
$zip->addEmptyDir($d);
} elseif (is_file($file) === true) {
$zip->addFromString(str_replace($source . DIRECTORY_SEPARATOR, '', $file),
file_get_contents($file));
} else {
// do nothing
}
}
} elseif (is_file($source) === true) {
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
Указываем путь к папке которую архивируем и куда положить архив:
zip('/folder/to/compress/', './compressed.zip');Распаковка Zip архива на PHP
Указываем файл архива и папку для его распаковки:
$zip = new ZipArchive;
$res = $zip->open('file.zip');
if ($res === TRUE) {
$zip->extractTo('/myzips/extract_path/');
$zip->close();
}