Domanda

Magento 2 offers \Magento\Framework\Archive\Tar to handle TAR files and \Magento\Framework\Archive\Gz to handle Gz archives.

Does Magento 2 offer a solution to directly unpack .tar.gz files or do I have to do it myself by first unpacking the .gz and then the .tar?

È stato utile?

Soluzione

As of 2.3.3, unfortunately Magento does not offer any .tar.gz file handling.

I've created my own class by implementing \Magento\Framework\Archive\ArchiveInterfaceand using both \Magento\Framework\Archive\Gz and Magento\Framework\Archive\Tar:

<?php

namespace Vendor\Module\Helper\Archive;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Archive\ArchiveInterface;
use Magento\Framework\Archive\Gz;
use Magento\Framework\Archive\Tar;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Directory\WriteInterface;

class TarGz implements ArchiveInterface
{
    const BASE_DIR = 'tar-gz/';

    /**
     * @var Gz
     */
    private $gzArchive;

    /**
     * @var Tar
     */
    private $tarArchive;

    /**
     * @var WriteInterface
     */
    private $tmpDirectory;

    /**
     * TarGz constructor.
     *
     * @param Gz $gzArchive
     * @param Tar $tarArchive
     *
     * @param TemporaryFiles $temporaryFiles
     */
    public function __construct(Gz $gzArchive, Tar $tarArchive)
    {
        $this->gzArchive = $gzArchive;
        $this->tarArchive = $tarArchive;
        $this->tmpDirectory = $filesystem->getDirectoryWrite(DirectoryList::TMP);
        
        $this->initTmpDirectory();
    }

    /**
     * Pack file or directory.
     *
     * @param string $source
     * @param string $destination
     *
     * @return string
     * @throws FileSystemException
     */
    public function pack($source, $destination)
    {
        $tarDestination = $this->getTmpTarPath($destination);
        $tarArchive = $this->tarArchive->pack($source, $tarDestination);
        $path = $this->gzArchive->pack($tarArchive, $destination);

        $this->delete($tarArchive);

        return $path;
    }

    /**
     * Unpack file or directory.
     *
     * @param string $source
     * @param string $destination
     *
     * @return string
     * @throws FileSystemException
     */
    public function unpack($source, $destination)
    {
        $tarDestination = $this->getTmpTarPath($source);
        $tarArchive = $this->gzArchive->unpack($source, $tarDestination);
        $unpackedPath = $this->tarArchive->unpack($tarArchive, $destination);

        $this->delete($tarArchive);

        return $unpackedPath;
    }

    /**
     * @return void
     * @throws FileSystemException
     */
    private function initTmpDirectory()
    {
        if (!$this->tmpDirectory->isExist(self::BASE_DIR)) {
            $this->tmpDirectory->create(self::BASE_DIR);
        }
    }

    /**
     * @param string $path
     *
     * @return string
     */
    public function getTmpTarPath($path)
    {
        $tarFilename = preg_replace('/\.gz$/', '', basename($path));
        $tarPath = $this->tmpDirectory->getAbsolutePath(self::BASE_DIR . DIRECTORY_SEPARATOR . $tarFilename);

        return $tarPath;
    }

    /**
     * @param string $path
     *
     * @return bool
     * @throws FileSystemException
     */
    public function delete($path)
    {
        return $this->tmpDirectory->delete($path);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top