3

I have a list of image files. I need to archive those files in zip format and make the archived file downloadable. I have seen ArchiverZip class defined in the system module of Drupal 7. But I couldn't be able to create a zip file. How can I do this in Drupal 7 ? Is there any hooks or custom modules for this ? Please help me out with this.

UPDATED Following are the codes:

$zip = new ArchiverZip('archive.zip');
foreach($_POST['img'] as $fid):
    $query = db_select('file_managed', 'fn')
            ->condition('fid', $fid)
            ->fields('fn', array('filename'));
    $result = $query->execute();

    foreach($result as $row) {
        if(file_exists('sites/default/files/'.$row->filename)){
            var_dump($zip->add($base_path.'sites/default/files/'.$row->filename));
        }
    }
 endforeach;

And this reflects the following error

Exception: Cannot open archive.zip in *ArchiverZip->__construct()* (line 91 of basepath\modules\system\system.archiver.inc).

Please help me with this . .

8
  • What problems were you having with the ArchiverZip class? It seems like a pretty straight forward class to me (though I haven't used it)
    – 2pha
    Mar 18, 2013 at 12:15
  • 1
    @2pha please check the updated content . . Mar 18, 2013 at 12:24
  • 1
    Try including the ZIP class to your function module_load_include('inc', 'zip_archive', 'zip_archive.class');
    – niksmac
    Mar 18, 2013 at 12:28
  • 1
    Please check Drupal log to see if there is any clue. also check archive.zip permissions.
    – niksmac
    Mar 18, 2013 at 12:35
  • 1
    check this drupal.org/node/83253
    – niksmac
    Mar 18, 2013 at 12:38

2 Answers 2

3

Make sure you include the class

module_load_include('inc', 'zip_archive', 'zip_archive.class');

Also check permissions for archive.zip file. Create one if its not there.

8
  • There is one thing need to ask you more buddy . . How can i arrange all the files in the same folder as files inside the zipped file are located like archive.zip\sites\default\files\ Mar 18, 2013 at 12:48
  • I need to arrange the files like archive.zip\ Mar 18, 2013 at 12:49
  • Add the files to zip folder as they exists in the disk.
    – niksmac
    Mar 18, 2013 at 12:51
  • I don't get you . . Will you explain more please ? ? Mar 18, 2013 at 12:53
  • I think you want the zip file with files exactly in your disk right ? if yes just add the files to zip as they are in disk. -r
    – niksmac
    Mar 18, 2013 at 12:55
1

I don't believe the accepted answer is correct. You don't need to include the class. To get this to work, you need to make sure that the file you are passing the ZipArchiver constructor already exists - that is where I believe the original question was getting the error:

Exception: Cannot open archive.zip

To make sure the file you are passing to the constructor already exists, try this (assuming the drupal path is something like a private or temp directory)

$filename = [drupal path] . [zip file name];
fopen(drupal_realpath($filename), 'w');
$zip = new ArchiverZip(drupal_realpath($filename));

We pass the path to drupal_realpath() so it converts the path from something like "private://filename" to "[my app]/sites/default/files/private/filename".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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