Laravel ZIP 目录和子目录

Kha*_*nem 3 php directory zip laravel

我有这段代码,但它只压缩文件,是否也可以包含其中的所有目录和文件?压缩文件应与包含以下内容的文件夹结构匹配:(就像在 PC 上压缩文件夹的正常操作一样)

- Documents
-- test.pdf
-- file.pdf
- Images
-- test.png
- mainfile.xsl
Run Code Online (Sandbox Code Playgroud)

代码如下:(取自不同的堆栈解决方案)

    // 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());
    }
Run Code Online (Sandbox Code Playgroud)

如果替代代码能够实现结果,那么它也是可以接受的。

Gle*_*nnM 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();
    }
Run Code Online (Sandbox Code Playgroud)

    /**
     * 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();
        }
    }

Run Code Online (Sandbox Code Playgroud)