ZipArchive zip unzip

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • item
    Active Community Member
    • Mar 2017
    • 1476

    ZipArchive zip unzip

    Hello Yuri,

    unzip is perfect
    zip not implemented.
    what do you think ?
    ps : concatPaths it's just a copy/past of fileManager
    sorry for indentation..

    PHP Code:
    
    /**
    * Concat paths.
    * @param string | array $paths Ex. array('pathPart1', 'pathPart2', 'pathPart3')
    * @return string
    */
    protected function concatPaths($paths)
    {
      if (is_string($paths)) {
       return Util::fixPath($paths);
      }
    
    $fullPath = '';
    
    foreach ($paths as $path) {
      $fullPath = Util::concatPath($fullPath, $path);
    }
    
     return $fullPath;
    }
    
    
    /**
    * Zip path
    *
    * @param [type] $sourcePath php8 string|array
    * @param string $fileName string
    * @return ?string $content of zip
    */
    public function zip($sourcePath, string $fileName) : ?string
    {
      if (!class_exists('\ZipArchive')) {
        throw new Error("Class ZipArchive does not installed. Cannot zip the file.");
      }
    
      $path = $this->concatPaths($sourcePath);
    
      if (!$path || !$fileName) {
        return null;
      }
    
      $fileManager = $this->getFileManager();
    
      $zip = new \ZipArchive;
      $zip->open('data/tmp/' .$fileName, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
    
      $fileList = $fileManager->getFileList($path);
    
      foreach($fileList as $theFile) {
       $zip->addFile($path .'/' .$theFile);
      }
    
      $zip->close();
    
      $content = $fileManager->getContents('data/tmp/' .$file);
    
      unlink('data/tmp/' .$file);
    
      return $content;
    } 
    
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​
  • yuri
    Member
    • Mar 2014
    • 8451

    #2
    There's no need to have this in fileManager. Before adding something into codebase we need to ask ourselves, do we really need it? In this case the answer is 'no'. Bloating of classes is not good for product maintenance. Maybe as a separate class. But it would be justifiable only we would need to use it in espo in multiple places.
    Last edited by yuri; 12-05-2021, 08:26 AM.
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment


    • item
      item commented
      Editing a comment
      Thanks Yuri for response. I understand and ok.

      Just for info, we have a job who each month make +200 pdf and some other files and i zip all and create a document with the zip as attachment

    • esforim
      esforim commented
      Editing a comment
      On top of my head, the biggest usage for this is to easily download multiple attachment. If an email only contain 2-3 attachments no problem; but once it get to 5+ it get annoying.

      For comparison; with big email provider (Google, Yahoo, Hotmail) and OSS email software (Roundcube) both offer "Download as a ZIP" option when it come to attachments.
  • ShulzR
    Member
    • Aug 2022
    • 32

    #3
    Has anyone managed to implement downloading all attachments as zip? It is very annoying and time-consuming when you need to download e.g. 10 photos from Stream.

    Comment


    • esforim
      esforim commented
      Editing a comment
      As a workaround I use a browser Extension call DownThemAll, would recommend you giving it a try. Until there is a build in function, it is the best method to save many files at once.

      I dread downloading 10+ PDF attachment in EspoCRM without using this extension.
Working...