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
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | zip-archiver-cant-create-new-files-2901618-8.patch | 616 bytes | patpluspun |
| #3 | provide_configuration_to_archivers_plugins-2901618-3.patch | 3.42 KB | nanak |
Comments
Comment #2
nanakThe 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.
Comment #3
nanakFix error - Replaces == by = in Zip file
Comment #8
patpluspun commentedHowdy, 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.
Comment #9
patpluspun commentedComment #10
msypes commentedI'm trying to use this functionality myself and have hit the same or similar snag:
Without the described flags, Drupal's
Zipclass 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
ZipCreatorclass before I realized Drupal had one, and could use that instead. Alternatively, I could extendZipand override the constructor, but if there's a way to use what's built-in, I'd prefer that.Comment #11
vierlexI 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.
Comment #12
mmenavas commentedI can attest that patch #8 works well!
What are the next steps to get this patch committed?
Comment #14
kevinfunkThis issue and #2850794 are trying to solve the same problem. I'm closing this one since #2850794 is older and has tests.