$path 是路径 比如:/www/wwwroot/abc.123.com/abc/   或  D://www/wwwroot/abc.123.com/abc/
$files 是压缩的文件和文件夹   array()数组转换
 $path=$_POST['p'];//压缩路径  
    $data= explode(',',$_POST['itemurl']);//压缩文件a.html,b.html
    $files = array();//压缩文件加入数组转换
    while(list($name,$value)=each($data)){
        $files[] = $value;
    }
   
    
    
    
    if (!empty($files)){
        chdir($path);
        $ext='zip';//压缩后缀
        
        if (PATH_URL == 1) {//判断是否自定义路径自定义路径
            $one_file = reset($files);
            
            $one_file = basename($one_file);
            
            $zipname = $one_file . '_' . date('YmdHis') . '.'.$ext;
        } else {
            $files1=PATH_URL."/".$path;
            $one_file1 = basename($files1);
            $zipname = $one_file1.'_'. date('YmdHis').'.'.$ext;
        }

            $zipper = new FM_Zipper();
            $res = $zipper->create($zipname, $files);
    
        if ($res) {
            exit(json_encode(array('code'=>0,'msg'=>'压缩成功!')));
            
        } else {
           
             exit(json_encode(array('code'=>1,'msg'=>'没找到文件')));
        }
    } else {
         echo '未选择任何内容!';
       
    }

function fm_get_zif_info($path, $ext) {

        $arch = zip_open($path);
        if ($arch) {
            $filenames = array();
            while ($zip_entry = zip_read($arch)) {
                $zip_name = zip_entry_name($zip_entry);
                $zip_folder = substr($zip_name, -1) == '/';
                $filenames[] = array(
                    'name' => $zip_name,
                    'filesize' => zip_entry_filesize($zip_entry),
                    'compressed_size' => zip_entry_compressedsize($zip_entry),
                    'folder' => $zip_folder,
                    'compression_method' => zip_entry_compressionmethod($zip_entry),
                );
            }
            zip_close($arch);
            return $filenames;
        }
  
    return false;
}

 }/*批量压缩 end*/
 
 
  
class FM_Zipper{
    private $zip;
    public function __construct(){
        $this->zip = new ZipArchive();
    }

    /**
     * 创建名为$filename的存档文件和名为$files的文件(相对路径!)
     * @参数字符串 $filename
     * @参数数组|字符串 $files
     * @return bool
     */
    public function create($filename, $files){
        $res = $this->zip->open($filename, ZipArchive::CREATE);
        if ($res !== true) {
            return false;
        }
        if (is_array($files)) {
            foreach ($files as $f) {
                if (!$this->addFileOrDir($f)) {
                    $this->zip->close();
                    return false;
                }
            }
            $this->zip->close();
            return true;
        } else {
            if ($this->addFileOrDir($files)) {
                $this->zip->close();
                return true;
            }
            return false;
        }
    }

    /**
     * 将归档文件$filename提取到文件夹$path(相对或绝对路径)
     * @param string $filename
     * @param string $path
     * @return bool
     */
    public function unzip($filename, $path){
        $res = $this->zip->open($filename);
        if ($res !== true) {
            return false;
        }
        if ($this->zip->extractTo($path)) {
            $this->zip->close();
            return true;
        }
        return false;
    }

    /**
     * 将文件/文件夹添加到存档
     * @param string $filename
     * @return bool
     */
    private function addFileOrDir($filename){
        if (is_file($filename)) {
            return $this->zip->addFile($filename);
        } elseif (is_dir($filename)) {
            return $this->addDir($filename);
        }
        return false;
    }

    /**
     * 递归添加文件夹
     * @param string $path
     * @return bool
     */
    private function addDir($path){
        if (!$this->zip->addEmptyDir($path)) {
            return false;
        }
        $objects = scandir($path);
        if (is_array($objects)) {
            foreach ($objects as $file) {
                if ($file != '.' && $file != '..') {
                    if (is_dir($path . '/' . $file)) {
                        if (!$this->addDir($path . '/' . $file)) {
                            return false;
                        }
                    } elseif (is_file($path . '/' . $file)) {
                        if (!$this->zip->addFile($path . '/' . $file)) {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        return false;
    }
}