解决方案 »

  1.   

    php压缩zip 
      

  2.   


    php 默认执行时间是30秒,  这个抛异常时, 不到5秒时间.
    ini文件中 memory_limit 设置成 512MB都没有用.
      

  3.   

    抛出的异常信息是什么?
    总得要据此判断问题的所在吧?
    我用你的程序压缩一个图片文件夹(包含 596 个文件,2 个文件夹)运行十余秒,并未发现任何异常
    另外在抛出异常时,应尽量带上 php 产生的错误信息
      

  4.   

    php错误配置为 error_reporting  =  E_ALL & ~E_NOTICE
    如果不用throw new Exception('添加文件失败,原始文件名:'.$path.'|新文件名:'.$fileinfo.$objZip->getStatusString() , '111');
    这种方式,  运行程序的话, 没有任何错误提示
    php版本是  5.2.3 在同事机器上的那个环境上运行,   文件夹有 420个文件, 能正常运行, 正常压缩 数据无丢失 .
    可能是环境配置问题,  具体是哪, 没有找出来.
      

  5.   

    如果不用 throw new Exception.... 就无任何错误提示
    那么你不是用了吗?又有什么提示呢?
      

  6.   

    是什么问题没有找到解决方法,  只能采用分部压缩了, 200个文件压缩一次
    提供代码   供参考
    <?php
    error_reporting(E_ALL);
    set_time_limit(0);
    header('Content-Type:text/html;charset=utf8');
    $fileArr = array();
    $basePath = dirname(__FILE__);

    $objZip = new ZipArchive();
    if($objZip->open('test.zip',  ZipArchive::CREATE)){
    createZip($basePath ,$objZip, '' ,$fileArr);
    }
    $objZip->close(); if(empty($fileArr)) die('没有数据');
    $index = 1;
    //一次压缩200个文件
    try{
    foreach ($fileArr as $key => $value){
    if(($index-1)%200==0){
    $objZip->open('test.zip',  ZipArchive::CREATE);
    }
    $objZip->addFile($value['orgFile'],$value['newFile']);
    if($index % 200 == 0 ){
    $objZip->close();
    }
    }
    if($index % 200 != 0){
    $objZip->close();
    }
    }catch(Exception $e){
    echo $e->getMessage();
    }


    /**
     * 压缩文件, 不带绝对文件目录
     * @param $basePath 要进行文件压缩的目录
     * @param $objZip 压缩包对象
     * @param $newDir 新目录,递归时用
     */
    function createZip($basePath, $objZip, $newDir='', &$fileArr=array()){
    foreach (new DirectoryIterator($basePath) as $fileinfo){
    $path = $basePath.DIRECTORY_SEPARATOR.$fileinfo;
    if($fileinfo != '.' && $fileinfo != '..'){
    $fileinfo = ($newDir == '' ? $fileinfo : $newDir.DIRECTORY_SEPARATOR.$fileinfo);
    if(is_file($path)){
    $fileArr[] = array('orgFile' => $path, 'newFile' => (string)$fileinfo);
    //echo 'aaa--'.$path.'<br>';
    // if($objZip->addFile($path,$fileinfo)){
    // //echo 'aaa--'.$path.'<br>';
    // }else{
    // //throw new Exception('添加文件失败,原始文件名:'.$path.'|新文件名:'.$fileinfo.$objZip->getStatusString() , '111');
    // }
    }else if(is_dir($path)){
    if($objZip->addEmptyDir($fileinfo)){
    //echo 'dir--'.$fileinfo.'<br>';
    }else{
    //throw new Exception('创建目录失败', '222');
    }
    createZip($path,$objZip, $fileinfo, $fileArr);
    }
    }
    }
    }
      

  7.   

    foreach 循环中 忘记把变量   $index递加了,   要不然, 每个文件都要打开ZIP包, 慢死