簡體   English   中英

使用 Flysystem 和 ZipAdapter 在 Symfony4 中創建 ZIP 文件時出錯

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

介紹

在我的個人項目中,我使用:

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 5 = read error

最后

我錯過了什么嗎?

謝謝你的想法和建議!

我設法創建了 ZIP 檔案,但沒有使用mount manager 所以問題是:“ZipAdapter 不應該/不應該與安裝管理器一起工作嗎?”

一個人在項目問題https://github.com/thephpleague/flysystem/issues/1009 中遇到了類似的問題。 基本上你不能將它與安裝管理器一起使用。

引用https://github.com/frankdejonge

'使用掛載管理器創建 zip 真的沒有意義。 如果您想以一種可移植的方式從遠程源創建 zip,您應該使用以下設置: 從中讀取文件的源文件系統。 您要在其中寫入最終 ZIP 的目標文件系統。 用於創建 zip 的 zip 文件系統。 您使用 zip 文件系統將文件寫入其中,這將始終位於本地位置。 將所有文件從源文件寫入 zip 后,您可以取消設置 zip 文件系統,這會導致寫入 zip 文件(PHP 的 ZipArchive 實現的限制)。 您現在可以從 zip 文件位置打開讀取流,並使用 writeStream 將其寫入目標文件系統。

此設置將使其與源文件系統和目標文件系統無關。 zip 將始終是本地的,其他文件系統無法制作 zip。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM