PHP实现压缩目录zip

Posted lonmy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP实现压缩目录zip相关的知识,希望对你有一定的参考价值。

第一步:创建MakeZip类文件

<?php
class MakeZip{

    /**
     * description:主方法:生成压缩包
     * @author: MY
     * @param $dir_path  想要压缩的目录:如 ‘./demo/‘
     * @param $zipName   压缩后的文件名:如 ‘./folder/demo.zip‘
     * @return string
     */
    function zip($dir_path, $zipName)
    { 
        $relationArr = array(
            $dir_path => array(
                ‘originName‘ => $dir_path,
                ‘is_dir‘ => true,
                ‘children‘ => array()
            )
        );

        $key = array_keys($relationArr);
        $val = array_values($relationArr);

        $this->modifiyFileName($dir_path, $relationArr[$dir_path][‘children‘]);
        $zip = new ZipArchive();
        //ZIPARCHIVE::CREATE没有即是创建
        $zip->open($zipName, ZipArchive::CREATE);
        $this->zipDir($key[0], ‘‘, $zip, $val[0][‘children‘]);
        $zip->close();
        $this->restoreFileName($key[0], $val[0][‘children‘]);
        return true;
    }

    function zipDir($real_path, $zip_path, &$zip, $relationArr)
    {
        $sub_zip_path = empty($zip_path) ? ‘‘ : $zip_path . ‘\‘;
        if (is_dir($real_path)) {
            foreach ($relationArr as $k => $v) {
                if ($v[‘is_dir‘]) {  //是文件夹
                    $zip->addEmptyDir($sub_zip_path . $v[‘originName‘]);
                    $this->zipDir($real_path . ‘\‘ . $k, $sub_zip_path . $v[‘originName‘], $zip, $v[‘children‘]);
                } else { //不是文件夹
                    $zip->addFile($real_path . ‘\‘ . $k, $sub_zip_path . $k);
                    $zip->deleteName($sub_zip_path . $v[‘originName‘]);
                    $zip->renameName($sub_zip_path . $k, $sub_zip_path . $v[‘originName‘]);
                }
            }
        }
    }

    function modifiyFileName($path, &$relationArr)
    {
        if (!is_dir($path) || !is_array($relationArr)) {
            return false;
        }
        if ($dh = opendir($path)) {
            $count = 0;
            while (($file = readdir($dh)) !== false) {
                if(in_array($file,array(‘.‘, ‘..‘, null))) continue; //无效文件,重来
                if (is_dir($path . ‘\‘ . $file)) {
                    $newName = md5(rand(0, 99999) . rand(0, 99999) . rand(0, 99999) . microtime() . ‘dir‘ . $count);
                    $relationArr[$newName] = array(
                        ‘originName‘ => iconv(‘GBK‘, ‘UTF-8‘, $file),
                        ‘is_dir‘ => true,
                        ‘children‘ => array()
                    );
                    rename($path . ‘\‘ . $file, $path . ‘\‘ . $newName);
                    $this->modifiyFileName($path . ‘\‘ . $newName, $relationArr[$newName][‘children‘]);
                    $count++;
                } else {
                    $extension = strchr($file, ‘.‘);
                    $newName = md5(rand(0, 99999) . rand(0, 99999) . rand(0, 99999) . microtime() . ‘file‘ . $count);
                    $relationArr[$newName . $extension] = array(
                        ‘originName‘ => iconv(‘GBK‘, ‘UTF-8‘, $file),
                        ‘is_dir‘ => false,
                        ‘children‘ => array()
                    );
                    rename($path . ‘\‘ . $file, $path . ‘\‘ . $newName . $extension);
                    $count++;
                }
            }
        }
    }

    function restoreFileName($path, $relationArr)
    {
        foreach ($relationArr as $k => $v) {
            if (!empty($v[‘children‘])) {
                $this->restoreFileName($path . ‘\‘ . $k, $v[‘children‘]);
                rename($path . ‘\‘ . $k, iconv(‘UTF-8‘, ‘GBK‘, $path . ‘\‘ . $v[‘originName‘]));
            } else {
                rename($path . ‘\‘ . $k, iconv(‘UTF-8‘, ‘GBK‘, $path . ‘\‘ . $v[‘originName‘]));
            }
        }
    }
}

第二步:调用index.php文件

<?php
/**
 * 下面代码实现把demo目录压缩成zip包,并以demo.zip为名称存储到folder目录
 */
include_once "./MakeZip.class.php";
$makeZip = new MakeZip();
try{
    //重复压缩,则会自动覆盖
    $res = $makeZip->zip(‘./demo/‘,‘./folder/demo.zip‘);
    if(!$res){
        throw new Exception(‘压缩失败‘);
    }
    echo ‘success‘;
}catch (Exception $e){
    echo $e->getMessage();
}

|--补充

MakeZip类文件中所实例化ZipArchive类 需开启PHP支持ZipArchive
在php.ini文件中将extension=php_zip.dll  开头的;的去掉。

以上是关于PHP实现压缩目录zip的主要内容,如果未能解决你的问题,请参考以下文章

php ZipArchive 压缩整个文件夹 - 自带ZipArchive类 - PHP递归创建目录压缩包

php ZipArchive 压缩整个文件夹 - 自带ZipArchive类 - PHP递归创建目录压缩包

PHP扩展类ZipArchive实现压缩Zip文件和文件打包下载

php实现ZIP压缩文件解压缩

php代码片段: sendFile/videoStream/sendEmail/phpexcel/ffmpeg/zip

压缩目录中的所有文件并下载生成的.zip