php 通过 ZipArchive 压缩,并处理中文后下载,前台用ajax下载

11 篇文章 0 订阅

官网
ZipArchive 类

ZipArchive implements Countable {
/* Properties */
int $status; //Zip存档的状态
int $statusSys; //Zip存档的系统状态
int $numFiles; //存档中的文件数
string $filename; //文件系统中的文件名
string $comment; //存注释
/* Methods */
addEmptyDir ( string $dirname ) : bool //添加一个新目录
addFile ( string $filename [, string $localname = NULL [, int $start = 0 [, int $length = 0 ]]] ) : bool //从给定路径将文件添加到ZIP存档
addFromString ( string $localname , string $contents ) : bool //使用文件内容将文件添加到ZIP存档
addGlob ( string $pattern [, int $flags = 0 [, array $options = array() ]] ) : bool //通过glob模式从目录添加文件
addPattern ( string $pattern [, string $path = "." [, array $options = array() ]] ) : bool //通过PCRE模式从目录添加文件
close ( void ) : bool //关闭活动存档(打开或新创建)
public count ( void ) : int //计算achive中的文件数
deleteIndex ( int $index ) : bool //使用其索引删除存档中的条目
deleteName ( string $name ) : bool //使用名称删除存档中的条目
extractTo ( string $destination [, mixed $entries ] ) : bool //提取存档内容
getArchiveComment ([ int $flags ] ) : string  //返回Zip存档注释
getCommentIndex ( int $index [, int $flags ] ) : string //使用条目索引返回条目的注释
getCommentName ( string $name [, int $flags ] ) : string //使用条目名称返回条目的注释
GetExternalAttributesIndex ( int $index , int &$opsys , int &$attr [, int $flags ] ) : bool //检索由其索引定义的条目的外部属性
getExternalAttributesName ( string $name , int &$opsys , int &$attr [, int $flags ] ) : bool //检索由条目名称定义的条目的外部属性
getFromIndex ( int $index [, int $length = 0 [, int $flags ]] ) : string //使用其索引返回条目内容
getFromName ( string $name [, int $length = 0 [, int $flags ]] ) : string //使用其名称返回条目内容
getNameIndex ( int $index [, int $flags ] ) : string //使用条目的索引返回条目的名称
getStatusString ( void ) : string //返回状态错误消息,系统和/或编码消息
getStream ( string $name ) : resource //获取由其名称定义的条目的文件处理程序(只读)
locateName ( string $name [, int $flags ] ) : int //返回档案中条目的索引
open ( string $filename [, int $flags ] ) : mixed //打开一个ZIP文件档案
renameIndex ( int $index , string $newname ) : bool //重命名由其索引定义的条目
renameName ( string $name , string $newname ) : bool //重命名由其名称定义的条目
setArchiveComment ( string $comment ) : bool //设置ZIP存档的注释
setCommentIndex ( int $index , string $comment ) : bool //设置由其索引定义的条目的注释
setCommentName ( string $name , string $comment ) : bool//设置由其名称定义的条目的注释
setCompressionIndex ( int $index , int $comp_method [, int $comp_flags = 0 ] ) : bool
setCompressionName ( string $name , int $comp_method [, int $comp_flags = 0 ] ) : bool // 设置由其索引定义的条目的压缩方法
setEncryptionIndex ( int $index , string $method [, string $password ] ) : bool//设置由其名称定义的条目的压缩方法
setEncryptionName ( string $name , int $method [, string $password ] ) : bool //设置由其索引定义的条目的加密方法
setExternalAttributesIndex ( int $index , int $opsys , int $attr [, int $flags ] ) : bool //设置由其索引定义的条目的外部属性
setExternalAttributesName ( string $name , int $opsys , int $attr [, int $flags ] ) : bool //设置由条目名称定义的条目的外部属性
public setPassword ( string $password ) : bool //设置活动存档的密码
statIndex ( int $index [, int $flags ] ) : array //获取由其索引定义的条目的详细信息
statName ( string $name [, int $flags ] ) : array //获取由其名称定义的条目的详细信息
unchangeAll ( void ) : bool //撤消存档中所做的所有更改
unchangeArchive ( void ) : bool //还原存档中完成的所有全局更改
unchangeIndex ( int $index ) : bool //将所有所做的更改还原为给定索引处的条目
unchangeName ( string $name ) : bool //将完成的所有更改还原为具有给定名称的条目
}

预定义常量

ZipArchive::CREATE (integer) //如果归档文件不存在,创建文件。
ZipArchive::OVERWRITE (integer) // 如果存档存在,则覆盖存档内容
ZipArchive::EXCL (integer) //如果存在则报错

压缩加下载
php代码

//注意这个下载的文件尺寸不能太大
 static function zip($archive_folder,$zipFile)
    {
        $mes = [];

        $let = is_file($zipFile);
        $zip = new \ZipArchive();
        if ($zip->open($zipFile, ZipArchive::OVERWRITE) === TRUE) {
            $dir = preg_replace('/[\/]{2,}/', '/', $archive_folder . "/");
            $zip->addEmptyDir($dir);

            $dirs = array($dir);
            while (count($dirs)) {
                $dir = current($dirs);
                $zip->addEmptyDir($dir);

                $dh = opendir($dir);
                while ($file = readdir($dh)) {
                    if ($file != '.' && $file != '..') {
                        if (is_file($dir . $file)) {
                            $zip->addFile($dir . $file, $dir . $file);
                            $zip->renameName($dir . $file,$dir . self::judgeName("中文.html"));
                        } elseif (is_dir($dir . $file)) {
                            $dirs[] = $dir . $file . "/";
                        }
                        $dsd = 'safd';
                    }
                }
                closedir($dh);
                array_shift($dirs);
            }

            $zip->close();
        }

        //self::del_dir($path);

        if (!file_exists($zipFile)) {
            $mes['name'] = "zip";
            $mes['message'] = "压缩失败";
            $mes['code'] = "404";
        }else{
            $mes['name'] = "zip";
            $mes['message'] = "压缩成功";
            $mes['code'] = "200";
        }
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename=' . basename($zipFile)); //文件名
        //header("Content-Type: application/zip"); //zip格式的
        header('Content-Type: application/octet-stream'); //防止乱码
        header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
        header('Content-Length: ' . filesize($zipFile)); //告诉浏览器,文件大小
        @readfile($zipFile);
        return mes;
    }

static function judgeName($str)
    {
          if (!Text::isUTF8($str)) {
            $str = utf8_encode($str);
        }
        return  iconv("UTF-8", "GBK//IGNORE", $str);
    }

ajax

  function ajax(url) {
            let xhr;
            if (window.XMLHttpRequest) {
                //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
                xhr = new XMLHttpRequest();
            } else {
                // IE6, IE5 浏览器执行代码
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xhr.open("POST", url/*url地址*/, true);
            xhr.setRequestHeader("X-CSRF-TOKEN",$('meta[name="csrf-token"]').attr('content'));
            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xhr.responseType = "blob";
            xhr.onload = function () {
                if (this.status === 200 ) {
                    var blob = this.response;
                    var reader = new FileReader();
                    reader.readAsDataURL(blob);
                    reader.onload = function(ev) {//读入完成事件
                        var a = document.createElement("a");
                        a.href = ev.target.result;//读取结果
                        a.download = "挂牌.zip";
                        document.body.appendChild(a);
                        a.click();
                        a.remove();
                    }
                }
            }
            let id = [1,3,5];
            let ads = ['ad','asd','asdf'];
            xhr.send("id="+id.toString() + "&ads=" + ads.toString());

        };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值