简体   繁体   中英

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.

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 .

My code

Controller that creates ZIP file

<?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

As ZIP file creation goes on in public folder, there should be no concerns about lack of access.

Update 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.

Update 3

Found that ZipAdapter uses PHP ZipArchive . There are error codes in documentation. So my problem: 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 . So the question stands: "Do ZipAdapter is not supposed/made to work with mount manager?"

A guy had a similar problem in the project issues https://github.com/thephpleague/flysystem/issues/1009 . Basically you can not use it with the mount manager.

Quoting https://github.com/frankdejonge :

'It doesn't really make sense to create a zip using mount manager. 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. A destination filesystem where you want to write the eventual ZIP. A zip filesystem to create the zip. You use the zip filesystem to write the files to, this will always be at a local location. 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). You can now open a read stream from the zip file location, and use writeStream to write it to the destination filesystem.

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.'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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