How to verify File is Archive or not using Magento 2.

Check Given file is an archive or not using Magento 2 by Magento\Framework\Archive Class.

Archive class has a logic to verify the given file is an archive or not in Magento 2.

The archive file gives results as a boolean based on the file extension.

If the file contains a given list of path extensions, it will return true otherwise return false.

Valid Path extension for the file to consider as an archive,

'tar'
'gz'
'gz'
'tar.gz'
'tar.gz'
'bz'
'bz'
'bz'
'bz'
'tar.bz'
'tar.bz'
'tar.bz'
'tar.bz'

If filename is test.zip, it’s not considered as an archive.
If filename is test.tar, test.tar.gz, test.bz, test.gz, its valid archive file.

<?php
namespace Jesadiya\Archive\Model;

use Magento\Framework\Archive;

class VerifyArchive
{
    /**
     * @var Archive
     */
    public $archive;

    public function __construct(
        Archive $archive
    ) {
        $this->archive = $archive;
    }

    public function verifyArchive()
    {
        // Result false
        $fileName = 'test.zip';
        if ($this->archive->isArchive($fileName)) {
            // call custom function here...
        }

        // Result true
        $fileName = 'test.tar';
        if ($this->archive->isArchive($fileName)) {
            // call custom function here...
        }
    }
}

You need to pass the absolute path of the archive file to the isArchive() function to verify your file is an archive or not using Magento 2.

You can verify the archive file with passing filename to the above method and will return a response as boolean if the file is a valid archive.