<?php

/* ========================================================================
 * $Id: zip.php 1437 2018-08-28 16:49:26Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
class onezphp_zip extends onezphp{
  function __construct(){
    
  }
  /**
  * 将数组加入zip压缩包
  * @param undefined $files
  * 
  * @return
  */
  function zip($files){
    include_once(dirname(__FILE__).'/lib/phpzip.php');
    #新版
    if(class_exists('ZipArchive')){
      $zip = new ZipArchive();
      $zipname=ONEZ_CACHE_PATH.'/temp.zip';
      @unlink($zipname);
      if(!$zip->open($zipname,ZIPARCHIVE::CREATE)){
      }
      foreach($files as $k=>$v){
        $zip->addFromString(onez()->iconv('utf-8', 'gbk',$k),$v);
      }
      
      $zip->close();
      $this->data=onez()->read($zipname);  
      return $this;
    }
    
    $this->zip=new phpzip();
    foreach($files as $k=>$v){
      $this->zip->addfile($v,$k);
    }
    $this->data=$this->zip->zip();
    
    return $this;
  }
  /**
  * 获取zip压缩包的二进制数据
  * 
  * @return
  */
  function data(){
    return $this->zip->zip();
  }
  function unzip($file){
    $files=array();
    $zip = zip_open($file);
    if ($zip) {
      while ($zip_entry = zip_read($zip)) {
        $name=zip_entry_name($zip_entry);
        if (zip_entry_open($zip, $zip_entry, "r")) {
          $files[$name]=zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
          zip_entry_close($zip_entry);
        }
      }
      zip_close($zip);
    }
    $this->files=$files;
    return $this;
  }
}