PHP实现zip压缩的代码

直接上代码

/**
 * Class xZip
 *
 * $xZip = new xZip;
 * $xZip->zip(null, 'D:/www/log/index.php', 'D:/www/log/index.zip');
 * $xZip->zipAll(array('D:/www/log/index.php','D:/www/log/lottery.sql'),  'D:/www/log/index_all.zip');
 *
 */
class xZip
{
    public function __construct()
    {
    }


    /**
     * add multi file to ZIP file
     * @param array $source will be add to ZIP file list.
     * @param null $destination ZIP file path
     * @return null $destination
     * @throws Exception
     */
    public function zipAll($source = array(), $destination = null, $originPath = '')
    {
        if ($destination == null) {
            throw new Exception('param $destination must be not null.');
        }
        if (count($source) == 0) {
            throw new Exception('must be added one file.');
        }
        $zip = new ZipArchive;
        @mkdir($this->getParentPath($destination), 0777, true);
        $res = $zip->open($destination, ZipArchive::CREATE);
        if ($res != true) {
            throw new Exception('open zip(' . $destination . ') file error[' . $res . '].');
        }
        foreach ($source as $path) {
            $zip->addFile($path, iconv('UTF-8', 'GB2312', str_replace($originPath, NULL, $path)));
        }
        $zip->close();
        return $destination;
    }

    /**
     * add one file to ZIP file.
     * @param null $zip @ZipArchive object
     * @param null $source will be add to ZIP file.
     * @param null $destination dest ZIP file.
     * @return null $destination
     * @throws Exception
     */
    public function zip($zip = null, $source = null, $destination = null)
    {
        if ($destination == null) {
            throw new Exception('param $destination must be not null.');
        }
        if ($source == null) {
            throw new Exception('must be added one file.');
        }
        if ($zip == null) {
            $zip = new ZipArchive;
            @mkdir($this->getParentPath($destination), 0777, true);
            $res = $zip->open($destination, ZipArchive::CREATE);
            if ($res != true) {
                throw new Exception('open zip(' . $destination . ') file error[' . $res . '].');
            }
        }
        //iconv('UTF-8','GB2312',str_replace($this->getParentPath($source).'/', NULL, $source)
        $zip->addFile($source, str_replace($this->getParentPath($source) . '/', NULL, $source));
        $zip->close();
        return $destination;
    }

    /**
     * @param $source zip file path
     * @param $destination dest directory path
     */
    public function unzip($source, $destination)
    {
        @mkdir($destination, 0777, true);
        $zip = new ZipArchive;
        if ($zip->open($source) === true) {
            $zip->extractTo($destination);
            $zip->close();
        }
    }

    public function __destruct()
    {
    }

    private function getParentPath($destination)
    {
        $path = substr($destination, 0, strripos($destination, '/'));
        return $path;
    }
}

Leave a Comment