Unpacker.php 973 Bytes
Newer Older
jiangkebao's avatar
jiangkebao committed
1 2 3
<?php

namespace Meibuyu\Micro\Tools;
jiangkebao's avatar
jiangkebao committed
4 5
use Dimsav\UnixZipper;
use Meibuyu\Micro\Exceptions\HttpResponseException;
jiangkebao's avatar
jiangkebao committed
6 7 8 9 10 11 12 13 14 15 16 17

/**
 * 打包器
 * Class PackFiler
 */
class Unpacker
{
    //文件列表 一位数组
    public $fileList;
    //文件名
    public $fileName;

jiangkebao's avatar
jiangkebao committed
18
    public function __construct(array $fileList, $fileName)
jiangkebao's avatar
jiangkebao committed
19 20 21 22 23 24 25 26 27 28 29 30
    {
        $this->fileList = $fileList;
        $this->fileName = $fileName;
    }

    /**
     * 打包
     * @return string
     * @throws \Exception
     */
    public function unpack()
    {
jiangkebao's avatar
jiangkebao committed
31 32 33 34 35 36 37 38 39 40 41 42 43
        if (count($this->fileList) > 0) {
            $zipper = new UnixZipper();
            $filePath = $this->fileName.'.zip';
            foreach ($this->fileList as $k => $v) {
                $zipper->add($v);
            }
            $zipper->setDestination($filePath);
            $zipper->compress();
            return $filePath;
        } else {
            throw new HttpResponseException('打包失败,请重试');
        }

jiangkebao's avatar
jiangkebao committed
44 45
    }
}