简体   繁体   English

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

[英]Error while creating ZIP file in Symfony4 with Flysystem and ZipAdapter

Introduction介绍

In my personal project i am using:在我的个人项目中,我使用:

File operations with Flysystem works fine both in project_dir/public folder and in project_dir/data folders. Flysystem 的文件操作在project_dir/public文件夹和project_dir/data文件夹中都可以正常工作。

Problem问题

When i try to create ZIP archive in public directory there is an error: Could not open zip archive at:zip:\\\\test123\\my_zip_test.zip, error: 5 .当我尝试在public目录中创建 ZIP 存档时出现错误: Could not open zip archive at:zip:\\\\test123\\my_zip_test.zip, error: 5

My code我的代码

Controller that creates ZIP file创建 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', []);
    }
}

Update 1更新 1

As ZIP file creation goes on in public folder, there should be no concerns about lack of access.由于 ZIP 文件在public文件夹中创建,因此无需担心无法访问。

Update 2更新 2

As i am using Flysystem mount manager to easily manage files across same filesystem but different locations i would like to use the same setup also for ZIP files.由于我使用Flysystem mount manager来轻松管理同一文件系统但不同位置的文件,因此我想对 ZIP 文件也使用相同的设置。

Update 3更新 3

Found that ZipAdapter uses PHP ZipArchive .发现ZipAdapter使用PHP ZipArchive There are error codes in documentation.文档中有错误代码 So my problem: error 5 = read error所以我的问题: error 5 = read error

Finally最后

Am i missing something?我错过了什么吗?

Thank you for your ideas and suggestions!谢谢你的想法和建议!

I managed to crate ZIP archives, but without utilizing mount manager .我设法创建了 ZIP 档案,但没有使用mount manager So the question stands: "Do ZipAdapter is not supposed/made to work with mount manager?"所以问题是:“ZipAdapter 不应该/不应该与安装管理器一起工作吗?”

A guy had a similar problem in the project issues https://github.com/thephpleague/flysystem/issues/1009 .一个人在项目问题https://github.com/thephpleague/flysystem/issues/1009 中遇到了类似的问题。 Basically you can not use it with the mount manager.基本上你不能将它与安装管理器一起使用。

Quoting https://github.com/frankdejonge :引用https://github.com/frankdejonge

'It doesn't really make sense to create a zip using mount manager. '使用挂载管理器创建 zip 真的没有意义。 If you want to have a portable way of creating zips even from remote sources you should use the following setup: A source filesystem from where to read the files from.如果您想以一种可移植的方式从远程源创建 zip,您应该使用以下设置: 从中读取文件的源文件系统。 A destination filesystem where you want to write the eventual ZIP.您要在其中写入最终 ZIP 的目标文件系统。 A zip filesystem to create the zip.用于创建 zip 的 zip 文件系统。 You use the zip filesystem to write the files to, this will always be at a local location.您使用 zip 文件系统将文件写入其中,这将始终位于本地位置。 After writing all files from the source to the zip you can unset the zip filesystem, this causes the write of the zip file (limitation of PHP's ZipArchive implementation).将所有文件从源文件写入 zip 后,您可以取消设置 zip 文件系统,这会导致写入 zip 文件(PHP 的 ZipArchive 实现的限制)。 You can now open a read stream from the zip file location, and use writeStream to write it to the destination filesystem.您现在可以从 zip 文件位置打开读取流,并使用 writeStream 将其写入目标文件系统。

This setup will make it agnostic of the source and destination filesystem.此设置将使其与源文件系统和目标文件系统无关。 The zip will ALWAYS be local, the other filesystems can't make the zips.' zip 将始终是本地的,其他文件系统无法制作 zip。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM