Problem/Motivation

While using the ArchiverManager Service to create a ZIP file, I stumbled across the /Drupal/Core/Archiver/Zip.php which doesn't support flags for ZipArchiver. These flags are usefull to tell ZipArchiver to create the file if it doesn't exist.

public function __construct($file_path) {
    $this->zip = new \ZipArchive();
    // this returned true for me
    // $test = $this->zip->open($file_path, \ZipArchive::CREATE);
    if ($this->zip->open($file_path) !== TRUE) {
      throw new ArchiverException(t('Cannot open %file_path', ['%file_path' => $file_path]));
    }
  }

Proposed resolution

There seems to be a possibility within the ArchiveManager when creating the Archiver Instance..

  public function createInstance($plugin_id, array $configuration = []) {
    $plugin_definition = $this->getDefinition($plugin_id);
    $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition, 'Drupal\Core\Archiver\ArchiverInterface');
    return new $plugin_class($configuration['filepath']);
  }

the $configuration variables 'filepath' is all what gets used, so maybe there can something be passed along to the Zip Archiver.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

vierlex created an issue. See original summary.

nanak’s picture

Version: 8.3.6 » 8.5.x-dev
Status: Active » Needs review
StatusFileSize
new3.42 KB

The attached patch updates the ArchiverManager by passing a configuration array to the plugin (Zip or Tar) instead of the filepath only. Zips can now receive a flag to use when the open() method is called in the constructor, and Tar can receive the compression format. Both parameters are still optional.

nanak’s picture

StatusFileSize
new3.42 KB

Fix error - Replaces == by = in Zip file

Status: Needs review » Needs work

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

patpluspun’s picture

Howdy, I recently stumbled across this issue myself, and considered using the attached patch, but a much simpler solution exists in just adding the default flags for overwriting/creating a new zip archive. Given a file path, the class will now either update an existing zip if found in the directory, or create a new one if not found.

patpluspun’s picture

Status: Needs work » Needs review
msypes’s picture

I'm trying to use this functionality myself and have hit the same or similar snag:
Without the described flags, Drupal's Zip class can't/won't create the zip file. With the flags, it does.

I would like to know if there's a proper way to use this class as-is while waiting for this patch to make it into core.

I rolled my own ZipCreator class before I realized Drupal had one, and could use that instead. Alternatively, I could extend Zip and override the constructor, but if there's a way to use what's built-in, I'd prefer that.

vierlex’s picture

I would like to know if there's a proper way to use this class as-is while waiting for this patch to make it into core.

I don't see how you could since you got the problem this issue describes.
To me, being able to pass along flags for the actual zip/tar interface seems the proper way with patch #3
If no flags are passed, then there should be default case, similar like #8

So a combination of both should be the way to go.

mmenavas’s picture

I can attest that patch #8 works well!

What are the next steps to get this patch committed?

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kevinfunk’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#2850794: Unable to open Zip archive using ArchiverZip

This issue and #2850794 are trying to solve the same problem. I'm closing this one since #2850794 is older and has tests.