我在服务器中创建一个 zip 文件并下载。 它可以通过 winrar 解压来工作
file
|__test1
|__ test04.txt
但是当我使用从window服务器下载文件的7zip解压时(它可以与从centos下载文件一起使用),结果很奇怪。
file
|__test1
|__test1.Test04.txt
if ($zip->open($filePath, \ZipArchive::CREATE) !== true) {
throw new \RuntimeException('Cannot open ' . $filePath);
}
$this->addContent($zip, (Storage::path($rootFolder . '/' . $folderName)));
$zip->close();
private function addContent(\ZipArchive $zip, string $path)
{
/** @var SplFileInfo[] $files */
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$path,
FilesystemIterator::FOLLOW_SYMLINKS
),
RecursiveIteratorIterator::SELF_FIRST
);
while ($iterator->valid()) {
if (!$iterator->isDot()) {
$filePath = $iterator->getPathName();
$relativePath = substr($filePath, strlen($path) + 1);
if (!$iterator->isDir()) {
$zip->addFile($filePath, $relativePath);
} else {
if ($relativePath !== false) {
$zip->addEmptyDir($relativePath);
}
}
}
$iterator->next();
}
}
我尝试用其他常量替换 SELF_FIRST 但不起作用
感谢您的帮助
substr
永远不会返回 false,但您还必须有一些文件/目录名称,因此 if ($relativePath !== false) {}
是不必要的。不要认为您的代码有问题,一定有错误,无论是 Windows 版本的 php(对于 8.2 其模块)使用的 7zip 或 libzip 中。您可以尝试手动(不使用递归)创建测试存档,以查看您的递归迭代器不应该受到指责。升级(降级?)你的 7zip/php/php_zip