You are here

protected function WebformExporterBase::addToZipFile in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformExporterBase.php \Drupal\webform\Plugin\WebformExporterBase::addToZipFile()

Add file, directory, or content to ZIP file.

Parameters

string $path: System path or file content.

string $name: Archive path or file name (applies to file content).

array $options: Zip file options.

1 call to WebformExporterBase::addToZipFile()
WebformExporterBase::addToArchive in src/Plugin/WebformExporterBase.php
Add file, directory, or content to exporter archive.

File

src/Plugin/WebformExporterBase.php, line 381

Class

WebformExporterBase
Provides a base class for a results exporter.

Namespace

Drupal\webform\Plugin

Code

protected function addToZipFile($path, $name, array $options = []) {
  if (!isset($this->archive)) {
    $this->archive = new \ZipArchive();
    $flags = !file_exists($this
      ->getArchiveFilePath()) ? \ZipArchive::CREATE : NULL;
    $this->archive
      ->open($this
      ->getArchiveFilePath(), $flags);
  }
  if (@file_exists($path)) {
    if (is_dir($path)) {

      // Add directory to ZIP file.
      $options += [
        'add_path' => $name . '/',
      ];
      $this->archive
        ->addPattern('/\\.[a-z0-9]+$/', $path, $options);
    }
    else {

      // Add file to ZIP file.
      // Get file name from the path and remove path option.
      $file_name = $path;
      if ($options['remove_path']) {
        $file_name = preg_replace('#^' . $options['remove_path'] . '#', '', $file_name);
      }
      $file_name = ltrim($file_name, '/');
      $this->archive
        ->addFile($path, $name . '/' . $file_name);
    }
  }
  else {

    // Add text to ZIP file.
    $this->archive
      ->addFromString($name, $path);
  }

  // Close and reset the ZIP file.
  // @see \Drupal\webform\WebformSubmissionExporter::writeExportToArchive
  if (!empty($options['close'])) {
    $this->archive
      ->close();
    $this->archive = NULL;
  }
}