如何压缩 PHP 中的整个文件夹,甚至是空文件夹?
How to ZIP an entire folder in PHP, even the empty ones?
我正在尝试从我的应用程序 (Laravel 5.8) 上的文件夹下载一个 zip,它正在工作,但它们会跳过所有空文件夹,我需要它们在 zip 中。
我已经尝试过 ZipArchive (php) 和来自 composer 的 chumper/zipper。
知道我该怎么做吗?
这是一个 Linux 服务器,运行 MySQL 5,PHP 7.2 和 Apache2。
$files = glob($folder_path);
\Zipper::make($folder_path.'/'.$folder_main_name.'.zip')->add($files)->close();
这个库只接受 glob,但是如果你有任何可行的解决方案,我可以很容易地放弃它。
// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Is this a directory?
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
else {
$end2 = substr($file,-2);
if ($end2 == "/.") {
$folder = substr($file, 0, -2);
$zip->addEmptyDir($folder);
}
}
}
// Zip archive will be created only after closing object
$zip->close();
** 试试这个。关闭我的头顶,未经测试。但这是总体思路。
// Initialize archive object
$zip = new ZipArchive();
$zip->open($zipFilename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file)
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
if (!$file->isDir())
{
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}else {
if($relativePath !== false)
$zip->addEmptyDir($relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
你好。我修改了之前的答案,现在我得到了包含空文件夹的 zip 文件。希望对您有所帮助。
我正在尝试从我的应用程序 (Laravel 5.8) 上的文件夹下载一个 zip,它正在工作,但它们会跳过所有空文件夹,我需要它们在 zip 中。
我已经尝试过 ZipArchive (php) 和来自 composer 的 chumper/zipper。
知道我该怎么做吗?
这是一个 Linux 服务器,运行 MySQL 5,PHP 7.2 和 Apache2。
$files = glob($folder_path);
\Zipper::make($folder_path.'/'.$folder_main_name.'.zip')->add($files)->close();
这个库只接受 glob,但是如果你有任何可行的解决方案,我可以很容易地放弃它。
// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Is this a directory?
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
else {
$end2 = substr($file,-2);
if ($end2 == "/.") {
$folder = substr($file, 0, -2);
$zip->addEmptyDir($folder);
}
}
}
// Zip archive will be created only after closing object
$zip->close();
** 试试这个。关闭我的头顶,未经测试。但这是总体思路。
// Initialize archive object
$zip = new ZipArchive();
$zip->open($zipFilename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file)
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
if (!$file->isDir())
{
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}else {
if($relativePath !== false)
$zip->addEmptyDir($relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
你好。我修改了之前的答案,现在我得到了包含空文件夹的 zip 文件。希望对您有所帮助。