Laravel ZIP 目录及其子目录

3
我有这段代码,但它只压缩文件,是否可能将所有目录和其中的文件也包含在内呢?压缩文件应该匹配包含以下内容的文件夹结构:(就像在您的PC上压缩文件夹的常规操作一样)
- Documents
-- test.pdf
-- file.pdf
- Images
-- test.png
- mainfile.xsl

以下是代码:(摘自不同的堆栈解决方案)

    // Create a list of files that should be added to the archive.
    $files = glob(storage_path("app/course/*.*"));

    // Define the name of the archive and create a new ZipArchive instance.
    $archiveFile = storage_path("app/course/files.zip");
    $archive = new ZipArchive();

    // Check if the archive could be created.
    if ($archive->open($archiveFile, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
        // Loop through all the files and add them to the archive.
        foreach ($files as $file) {
            if ($archive->addFile($file, basename($file))) {
                // Do something here if addFile succeeded, otherwise this statement is unnecessary and can be ignored.
                continue;
            } else {
                throw new Exception("File [`{$file}`] could not be added to the zip file: " . $archive->getStatusString());
            }
        }

        // Close the archive.
        if ($archive->close()) {
            // Archive is now downloadable ...
            return response()->download($archiveFile, basename($archiveFile))->deleteFileAfterSend(true);
        } else {
            throw new Exception("Could not close zip file: " . $archive->getStatusString());
        }
    } else {
        throw new Exception("Zip file could not be created: " . $archive->getStatusString());
    }

如果它可以达到结果,另一种代码也是可以接受的。

@KurtFriars 是的,那个方法可行,而且很有道理。 - RG Servers
1个回答

8

最近我创建了这样的功能(即使在源目录中有符号链接时,它也应该可以正常工作)。

  1. 我创建归档文件
  2. 在创建过程中,我将一个目录的内容添加到归档中
  3. 最后,我关闭了这个归档。
    /**
     * @throws RuntimeException If the file cannot be opened
     */
    public function create()
    {
        $filePath = 'app/course/files.zip';
        $zip = new \ZipArchive();
    
        if ($zip->open($filePath, \ZipArchive::CREATE) !== true) {
            throw new \RuntimeException('Cannot open ' . $filePath);
        }
    
        $this->addContent($zip, realpath('app/course'));
        $zip->close();
    }


    /**
     * This takes symlinks into account.
     *
     * @param ZipArchive $zip
     * @param string     $path
     */
    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();
        }
    }


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接