示例#1
0
 private function open()
 {
     if ($this->file && file_exists($this->file)) {
         $this->files();
         $extension = pathinfo($this->file, PATHINFO_EXTENSION);
         $file = basename($this->file, '.' . $extension);
         if (pathinfo($file, PATHINFO_EXTENSION) != 'csv') {
             $file .= '.csv';
         }
         $path = pathinfo($this->file, PATHINFO_DIRNAME) . '/';
         $file = $path . $file;
         switch ($extension) {
             case 'gz':
                 if (extension_loaded('zlib')) {
                     if (function_exists('gzopen')) {
                         if (($src = gzopen($this->file, 'rb')) && ($dst = fopen($file, 'wb'))) {
                             stream_copy_to_stream($src, $dst);
                             gzclose($src);
                             fclose($dst);
                             $this->file = $file;
                         } else {
                             throw new waException("Error while read gz file");
                         }
                         $this->open();
                         break;
                     } elseif (in_array('compress.zlib', stream_get_wrappers())) {
                         $this->fp = fopen('compress.zlib://' . $this->file, 'rb');
                     } else {
                         throw new waException("Unsupported file extension");
                     }
                 } else {
                     throw new waException("Unsupported file extension");
                 }
                 $this->fsize = filesize($this->file);
                 break;
             case 'zip':
                 if (function_exists('zip_open') && ($zip = zip_open($this->file)) && is_resource($zip) && ($zip_entry = zip_read($zip))) {
                     //dummy read first file;
                     $file = $path . waLocale::transliterate(basename(zip_entry_name($zip_entry)));
                     $zip_fs = zip_entry_filesize($zip_entry);
                     if ($z = fopen($file, "w")) {
                         $size = 0;
                         while ($zz = zip_entry_read($zip_entry, max(0, min(4096, $zip_fs - $size)))) {
                             fwrite($z, $zz);
                             $size += 1024;
                         }
                         fclose($z);
                         zip_entry_close($zip_entry);
                         zip_close($zip);
                         $this->file = $file;
                         $this->files();
                     } else {
                         zip_entry_close($zip_entry);
                         zip_close($zip);
                         throw new waException("Error while read zip file");
                     }
                     $this->open();
                 } else {
                     throw new waException("Error while read zip file");
                 }
                 break;
             default:
                 if (is_array($this->encoding) || $this->encoding == 'auto') {
                     $this->fp = fopen($this->file, "rb");
                     if (!$this->fp) {
                         throw new waException("error while open CSV file");
                     }
                     $chunk = fread($this->fp, 4096);
                     if (is_array($this->encoding)) {
                         $this->encoding = mb_detect_encoding($chunk, $this->encoding);
                     } else {
                         $this->encoding = mb_detect_encoding($chunk);
                     }
                     if (strtolower($this->encoding) == 'utf-8') {
                         fseek($this->fp, 0);
                     }
                 }
                 if (strtolower($this->encoding) != 'utf-8') {
                     if ($this->fp) {
                         fclose($this->fp);
                         unset($this->fp);
                     }
                     if ($file = waFiles::convert($this->file, $this->encoding)) {
                         $this->encoding = 'utf-8';
                         $this->file = $file;
                         $this->files();
                         $this->fp = fopen($this->file, "rb");
                     } else {
                         throw new waException("Error while convert file encoding");
                     }
                 } elseif (!$this->fp) {
                     $this->fp = fopen($this->file, "rb");
                 }
                 $this->fsize = filesize($this->file);
                 break;
         }
     }
 }