/**
  * @param $zipFileName
  * @param $internalFiles ZipContentObject[]
  *
  * @return string
  */
 public function reBuildZipContent($zipFileName, $internalFiles)
 {
     $fs = new FilesStorage();
     $zipFile = $fs->getOriginalZipPath($this->jobInfo['create_date'], $this->jobInfo['id_project'], $zipFileName);
     $tmpFName = tempnam(INIT::$TMP_DOWNLOAD . '/' . $this->id_job . '/', "ZIP");
     copy($zipFile, $tmpFName);
     $zip = new ZipArchiveExtended();
     if ($zip->open($tmpFName)) {
         $zip->createTree();
         //rebuild the real name of files in the zip archive
         foreach ($zip->treeList as $filePath) {
             $realPath = str_replace(array(ZipArchiveExtended::INTERNAL_SEPARATOR, FilesStorage::pathinfo_fix($tmpFName, PATHINFO_BASENAME)), array(DIRECTORY_SEPARATOR, ""), $filePath);
             $realPath = ltrim($realPath, "/");
             //remove the tmx from the original zip ( we want not to be exported as preview )
             if (FilesStorage::pathinfo_fix($realPath, PATHINFO_EXTENSION) == 'tmx') {
                 $zip->deleteName($realPath);
                 continue;
             }
             //fix the file names inside the zip file, so we compare with our files
             // and if matches we can substitute them with the converted ones
             //                $fileName_fixed = array_pop( explode( DIRECTORY_SEPARATOR, str_replace( " ", "_", $realPath ) ) );
             foreach ($internalFiles as $index => $internalFile) {
                 //                    $__ourFileName = array_pop( explode( DIRECTORY_SEPARATOR, $internalFile->output_filename ) );
                 $_tmpRealPath = str_replace(array(" ", " "), "_", $realPath);
                 if ($internalFile->output_filename == $_tmpRealPath) {
                     $zip->deleteName($realPath);
                     if (FilesStorage::pathinfo_fix($realPath, PATHINFO_EXTENSION) == 'pdf') {
                         $realPath .= '.docx';
                     }
                     $zip->addFromString($realPath, $internalFile->getContent());
                 }
             }
         }
         $zip->close();
     }
     return $tmpFName;
 }
示例#2
0
 public function extractZipFile()
 {
     $this->file_name = html_entity_decode($this->file_name, ENT_QUOTES);
     $file_path = $this->intDir . DIRECTORY_SEPARATOR . $this->file_name;
     //The zip file name is set in $this->file_name
     $za = new ZipArchiveExtended();
     $za->open($file_path);
     try {
         $za->createTree();
         //get system temporary folder
         $tmpFolder = ini_get('upload_tmp_dir');
         empty($tmpFolder) ? $tmpFolder = "/tmp" : null;
         $tmpFolder .= "/" . uniqid('') . "/";
         mkdir($tmpFolder, 0777, true);
         $fileErrors = $za->extractFilesInTmp($tmpFolder);
         $za->close();
         //compose an array that has the same structure of $_FILES
         $filesArray = array();
         foreach ($za->treeList as $fileName) {
             $filesArray[$fileName] = array('name' => $fileName, 'tmp_name' => $tmpFolder . $fileName, 'error' => null, 'size' => filesize($tmpFolder . $fileName));
         }
         /***
          *
          * ERRORE di un file extratto dallo zip ( isset( $fileErrors[ $fileName ] ) ) ? $fileErrors[ $fileName ] :
          *
          **/
         // The $this->cookieDir parameter makes Upload get the upload directory from the cookie.
         // In this way it'll find the unzipped files
         $uploadFile = new Upload($this->cookieDir);
         $uploadFile->setRaiseException($this->stopOnFileException);
         try {
             $stdResult = $uploadFile->uploadFiles($filesArray);
             if ($this->uploadFailed($stdResult)) {
                 $this->uploadError = true;
                 $this->uploadedFiles = $stdResult;
             }
         } catch (Exception $e) {
             $stdResult = array();
             $this->result = array('errors' => array(array("code" => -1, "message" => $e->getMessage())));
             $this->api_output['message'] = $e->getMessage();
             return null;
         }
         return array_map("Upload::fixFileName", $za->treeList);
     } catch (Exception $e) {
         Log::doLog("ExtendedZipArchive Exception: {$e->getCode()} : {$e->getMessage()}");
         $this->result['errors'][] = array('code' => $e->getCode(), 'message' => "Zip error: " . $e->getMessage(), 'debug' => $this->file_name);
         return null;
     }
     return array();
 }