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;
    }
}

Linux 下zip包的压缩与解压

linux zip 命令详解
功能说明:压缩文件。
语 法:zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串>][-t <日期时间>][-<压缩效率>][压缩文件][文件…][-i <范本样式>][-x <范本样式>]
补充说明:zip是个使用广泛的压缩程序,文件经它压缩后会另外产生具有”.zip”扩展名的压缩文件。

 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |