ZipArchiveReader.php 1.75 KB
<?php
/**
 * Created by PhpStorm.
 * User: Tsurkanov
 * Date: 03.11.2015
 * Time: 15:12
 */

namespace common\components\archives;


class ZipArchiveReader extends  ArchiveReader {

    /**
     * @param $file - имя открываемого архива
     * @param string $password
     * @return bool
     * @throws \Exception
     */
    public  function open( $file, $password = '' ){

        $this->resource = new \ZipArchive;
        if ( $this->resource->open( $file ) === true )
            return true;

        $error_number = $this->resource;
            throw new \Exception("Failed opening zip file, error # {$error_number}");



    }

    /**
     * @param $destination - путь куда положить распакованные файлы
     * если указан аттрибут $file_name_prefix то происходит переименование файла
     * в результате выполнения метода заполняется аттрибут $extracted_files извлеченными файлами
     * если извлечение не произошло аттрибут $extracted_files - пустой
     */
    public  function extractTo( $destination ){

        for ($i = 0; $i < $this->resource->numFiles; $i++) {
            $filename = $this->resource->getNameIndex($i);
            $this->resource->renameName($filename,$this->file_name_prefix .$filename);
            $this->setExtractedFiles($destination . '/' . $filename, pathinfo($filename, PATHINFO_EXTENSION));

        }

        if (! $this->resource->extractTo( $destination ) ) {

            $this->extracted_files = [];

        }

        $this->resource->close();
    }

    public static  function getExtension(){
        return 'zip';
    }




}