2

How to create zip file programatically based on the files selected in Drupal 8? Is there any class already existing in Drupal 8?

As per below answer I'm using following code to generate zip file:

$file = \Drupal\file\Entity\File::load($fileId);
$fileUri = \Drupal::service('file_system')->realpath($file->getFileUri());

$zip = \Drupal::service('plugin.manager.archiver')->getInstance(['f‌​ilepath' => '/foo/bar.zip']);
$zip->add($fileUri);
  • The bar.zip file above should exist in the referenced path above: /foo/bar.zip.
  • Also how do I flatten the the zip file folder structure so that the file sites/default/files/myimage.png is myimage.png?
  • Yes. Archiver has existed since Drupal 7. – mradcliffe Jun 30 '17 at 12:43
3

Use the ArchiverManager and get the instance by calling the getInstance() method. A Zip class is returned. The Drupal Zip class uses PHP's ZipArchive class to perform the heavy lifting.

Example (edited after remark @Clive):

$file = \Drupal\file\Entity\File::load($fileId);
$fileUri = \Drupal::service('file_system')->realpath($file->getFileUri());

$zip = \Drupal::service('plugin.manager.archiver')->getInstance(['f‌​ilepath' => '/foo/bar.zip']);
$zip->add($fileUri);
  • 1
    Don't instantiate the object manually, always use services for this type of thing. $zip = \Drupal::service('plugin.manager.archiver')->getInstance(['filepath' => '/foo/bar.zip']); (and preferably inject the service if in a viable context too) – Clive Jun 29 '17 at 14:15
  • Thanks a lot Clive! Did not realize that it had a service. Updated my answer for future reference. – mvdgun Jun 29 '17 at 18:31
  • I tried your method mentioned above. But when run the code the 'bar.zip' file should exist in the mentioned the path. Also for the given fid the file adding to the zip file by including all the folder structure.. Inside zip file having following folder structure 'sites/defualt/files/myimage.png'. I only want that file alone inside zip file. Not all the file path folders. – ARUN Jun 30 '17 at 13:38
  • @Gun5m0k3, have any idea on my above comment? – ARUN Jul 3 '17 at 5:22
  • 1
    Just found the getArchive() method on the Zip class. So I would use the plugin.manager.archiver service, retrieve the PHP ZipArchive class by calling $zip->getArchive() and call the addFile() method on that. This way you are still using services which is better practice. – mvdgun Jul 3 '17 at 12:44
0

Below sample code is working fine to create zip file in Drupal 8.

$file = File::load($file_id);
$filename = $file->getFilename();
$file_path = \Drupal::service('file_system')->realpath($file->getFileUri());
$destination = \Drupal::service('file_system')->realpath('public://foldername');
$zip = new \ZipArchive;
$zip->open($destination . "\dump.zip", constant("ZipArchive::CREATE"));
$zip->addFile($file_path, $filename);
$zip->close();

Please check Backup and Migrate module soruce code for more reference.

Note :

  1. Create the destination folder before execute this above code.
  2. Please make sure php zip and gd libraries should be installed and enabled in the server.
  3. Recommended to use external zip creation library when the server doesn't support zipArchive class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.