使用Flysystem和ZipAdapter在Symfony4中创建ZIP文件时出错

问题描述 投票:0回答:1

介绍

在我使用的个人项目中:

Flysystem的文件操作在project_dir/public文件夹和project_dir/data文件夹中都可以正常工作。

问题

当我尝试在public目录中创建ZIP存档时出现错误:Could not open zip archive at:zip:\\test123\my_zip_test.zip, error: 5

我的代码

创建ZIP文件的控制器

<?php

namespace App\Controller;

use App\UltraHelpers\UltraAccess;
use App\UltraHelpers\UltraWhereabouts;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\MountManager;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;

class AdminZipController extends BaseController
{
    /**
     * @Route("/zip", name="zip")
     */
    public function createZipArchive(Request $request, SessionInterface $session, MountManager $mountManager, ContainerInterface $container)
    {

        $kernel_project_dir = $container->getParameter('kernel.project_dir');
        $adapter_public = new Local($kernel_project_dir .'/public/uploads/');
        $adapter_zip = new ZipArchiveAdapter($kernel_project_dir .'/public/uploads/');

        $filesystem_public = new Filesystem($adapter_public);
        $filesystem_zip = new Filesystem($adapter_zip);

        $mountManager->mountFilesystem('public', $filesystem_public);
        $mountManager->mountFilesystem('zip', $filesystem_zip);

        $public_path = 'public://test123/';
        $public_zip = 'zip://test123/my_zip_test.zip';

        $adapter_zip->openArchive($public_zip);

        // get all files in directory
        $files_2_zip = $mountManager->listContents($public_path, false);

        // itereate trough all files in directory
        foreach ($files_2_zip as $object)
        {
            $mountManager->copy($object['path'], $public_zip);
        }

        $adapter_zip->getArchive()->close();

        return $this->render('default/create_zip_archive.html.twig', []);
    }
}

更新1

随着ZIP文件的创建在public文件夹中进行,不应该担心缺少访问权限。

更新2

由于我使用Flysystem mount manager轻松管理相同文件系统中的文件但不同的位置我想对ZIP文件也使用相同的设置。

更新3

发现ZipAdapter使用PHP ZipArchive。文档中有error codes。所以我的问题:error 5 = read error

最后

我错过了什么吗?

感谢您的意见和建议!

php symfony zip symfony4 flysystem
1个回答
0
投票

我设法创建了ZIP档案,但没有使用mount manager。所以问题就在于:“不应该使用ZipAdapter /使其与mount管理器一起工作吗?”

© www.soinside.com 2019 - 2024. All rights reserved.