/**
  * Download (accessible) media for all records in this set
  */
 public function getSetMedia()
 {
     set_time_limit(600);
     // allow a lot of time for this because the sets can be potentially large
     $o_dm = Datamodel::load();
     $t_set = new ca_sets($this->request->getParameter('set_id', pInteger));
     if (!$t_set->getPrimaryKey()) {
         $this->notification->addNotification(_t('No set defined'), __NOTIFICATION_TYPE_ERROR__);
         $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_sets', $t_set->getPrimaryKey()));
         return false;
     }
     $va_record_ids = array_keys($t_set->getItemRowIDs(array('limit' => 100000)));
     if (!is_array($va_record_ids) || !sizeof($va_record_ids)) {
         $this->notification->addNotification(_t('No media is available for download'), __NOTIFICATION_TYPE_ERROR__);
         $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_sets', $t_set->getPrimaryKey()));
         return false;
     }
     $vs_subject_table = $o_dm->getTableName($t_set->get('table_num'));
     $t_instance = $o_dm->getInstanceByTableName($vs_subject_table);
     $qr_res = $vs_subject_table::createResultSet($va_record_ids);
     $qr_res->filterNonPrimaryRepresentations(false);
     $va_paths = array();
     while ($qr_res->nextHit()) {
         $va_original_paths = $qr_res->getMediaPaths('ca_object_representations.media', 'original');
         if (sizeof($va_original_paths) > 0) {
             $va_paths[$qr_res->get($t_instance->primaryKey())] = array('idno' => $qr_res->get($t_instance->getProperty('ID_NUMBERING_ID_FIELD')), 'paths' => $va_original_paths);
         }
     }
     if (sizeof($va_paths) > 0) {
         $o_zip = new ZipStream();
         foreach ($va_paths as $vn_pk => $va_path_info) {
             $vn_c = 1;
             foreach ($va_path_info['paths'] as $vs_path) {
                 if (!file_exists($vs_path)) {
                     continue;
                 }
                 $vs_filename = $va_path_info['idno'] ? $va_path_info['idno'] : $vn_pk;
                 $vs_filename .= "_{$vn_c}";
                 if ($vs_ext = pathinfo($vs_path, PATHINFO_EXTENSION)) {
                     $vs_filename .= ".{$vs_ext}";
                 }
                 $o_zip->addFile($vs_path, $vs_filename);
                 $vn_c++;
             }
         }
         $o_view = new View($this->request, $this->request->getViewsDirectoryPath() . '/bundles/');
         // send files
         $o_view->setVar('zip_stream', $o_zip);
         $o_view->setVar('archive_name', 'media_for_' . mb_substr(preg_replace('![^A-Za-z0-9]+!u', '_', ($vs_set_code = $t_set->get('set_code')) ? $vs_set_code : $t_set->getPrimaryKey()), 0, 20) . '.zip');
         $this->response->addContent($o_view->render('download_file_binary.php'));
         return;
     } else {
         $this->notification->addNotification(_t('No files to download'), __NOTIFICATION_TYPE_ERROR__);
         $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_sets', $t_set->getPrimaryKey()));
         return;
     }
     return $this->Edit();
 }
<?php

// Example. Zip all .html files in the current directory and save to current directory.
// Make a copy, also to the current dir, for good measure.
//$mem = ini_get('memory_limit');
$extime = ini_get('max_execution_time');
//
////ini_set('memory_limit', '512M');
ini_set('max_execution_time', 600);
include_once "ZipStream.php";
//print_r(ini_get_all());
$fileTime = date("D, d M Y H:i:s T");
$chapter1 = "Chapter 1\n" . "Lorem ipsum\n" . "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec magna lorem, mattis sit amet porta vitae, consectetur ut eros. Nullam id mattis lacus. In eget neque magna, congue imperdiet nulla. Aenean erat lacus, imperdiet a adipiscing non, dignissim eget felis. Nulla facilisi. Vivamus sit amet lorem eget mauris dictum pharetra. In mauris nulla, placerat a accumsan ac, mollis sit amet ligula. Donec eget facilisis dui. Cras elit quam, imperdiet at malesuada vitae, luctus id orci. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque eu libero in leo ultrices tristique. Etiam quis ornare massa. Donec in velit leo. Sed eu ante tortor.\n";
$zip = new ZipStream("ZipStreamExample1.zip");
$zip->setComment("Example Zip file for Large file sets.\nCreated on " . date('l jS \\of F Y h:i:s A'));
$zip->addFile("Hello World!\r\n", "Hello.txt");
$zip->openStream("big one3.txt");
$zip->addStreamData($chapter1 . "\n\n\n");
$zip->addStreamData($chapter1 . "\n\n\n");
$zip->addStreamData($chapter1 . "\n\n\n");
$zip->closeStream();
// For this test you need to create a large text file called "big one1.txt"
if (file_exists("big one1.txt")) {
    $zip->addLargeFile("big one1.txt", "big one2a.txt");
    $fhandle = fopen("big one1.txt", "rb");
    $zip->addLargeFile($fhandle, "big one2b.txt");
    fclose($fhandle);
}
$zip->addDirectory("Empty Dir");
//Dir test, using the stream option on $zip->addLargeFile
$fileDir = './';
 public function getLotMedia()
 {
     //if ((bool)$this->request->config->get('allow_download_of_all_object_media_in_a_lot')) {	// DO WE NEED TO REINSTATE THIS OPTIN?
     set_time_limit(600);
     // allow a lot of time for this because the sets can be potentially large
     $t_lot = new ca_object_lots($this->request->getParameter('lot_id', pInteger));
     $o_media_metadata_conf = Configuration::load($t_lot->getAppConfig()->get('media_metadata'));
     if ($t_lot->getPrimaryKey()) {
         $va_object_ids = $t_lot->get('ca_objects.object_id', array('returnAsArray' => true, 'limit' => 100000));
         if (!is_array($va_object_ids) || !sizeof($va_object_ids)) {
             $this->notification->addNotification(_t('No media is available for download'), __NOTIFICATION_TYPE_ERROR__);
             $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_object_lots', $t_lot->getPrimaryKey()));
             return;
         }
         $qr_res = ca_objects::createResultSet($va_object_ids);
         $qr_res->filterNonPrimaryRepresentations(false);
         $va_paths = array();
         while ($qr_res->nextHit()) {
             $va_original_paths = $qr_res->getMediaPaths('ca_object_representations.media', 'original');
             if (sizeof($va_original_paths) > 0) {
                 $va_paths[$qr_res->get('object_id')] = array('idno' => $qr_res->get('idno'), 'type_code' => caGetListItemIdno($qr_res->get('type_id')), 'paths' => $va_original_paths, 'representation_ids' => $qr_res->get('ca_object_representations.representation_id', array('returnAsArray' => true)), 'representation_types' => $qr_res->get('ca_object_representations.type_id', array('returnAsArray' => true)));
             }
         }
         if (sizeof($va_paths) > 0) {
             $o_zip = new ZipStream();
             foreach ($va_paths as $vn_object_id => $va_path_info) {
                 // make sure we don't download representations the user isn't allowed to read
                 if (!caCanRead($this->request->user->getPrimaryKey(), 'ca_objects', $vn_object_id)) {
                     continue;
                 }
                 $vn_c = 1;
                 foreach ($va_path_info['paths'] as $vn_i => $vs_media_path) {
                     if (!file_exists($vs_media_path)) {
                         continue;
                     }
                     if ($o_media_metadata_conf->get('do_metadata_embedding_for_lot_media_download')) {
                         if (!($vs_path = caEmbedMediaMetadataIntoFile($vs_media_path, 'ca_objects', $vn_object_id, $va_path_info['type_code'], $va_path_info['representation_ids'][$vn_i], $va_path_info['representation_types'][$vn_i]))) {
                             $vs_path = $vs_media_path;
                         }
                     } else {
                         $vs_path = $vs_media_path;
                     }
                     $vs_filename = $va_path_info['idno'] ? $va_path_info['idno'] : $vn_object_id;
                     $vs_filename .= "_{$vn_c}";
                     if ($vs_ext = pathinfo($vs_media_path, PATHINFO_EXTENSION)) {
                         $vs_filename .= ".{$vs_ext}";
                     }
                     $o_zip->addFile($vs_path, $vs_filename);
                     $vn_c++;
                 }
             }
             $o_view = new View($this->request, $this->request->getViewsDirectoryPath() . '/bundles/');
             // send files
             $o_view->setVar('zip_stream', $o_zip);
             $o_view->setVar('archive_name', 'media_for_' . mb_substr(preg_replace('![^A-Za-z0-9]+!u', '_', ($vs_idno = $t_lot->get('idno_stub')) ? $vs_idno : $t_lot->getPrimaryKey()), 0, 20) . '.zip');
             $this->response->addContent($o_view->render('download_file_binary.php'));
             return;
         } else {
             $this->notification->addNotification(_t('No files to download'), __NOTIFICATION_TYPE_ERROR__);
             $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_object_lots', $t_lot->getPrimaryKey()));
             return;
         }
     }
     //}
     return $this->Edit();
 }
 /**
  * 
  * 
  */
 public function DownloadRepresentations()
 {
     if ($t_subject = $this->opo_datamodel->getInstanceByTableName($this->ops_tablename, true)) {
         $o_media_metadata_conf = Configuration::load($t_subject->getAppConfig()->get('media_metadata'));
         $pa_ids = null;
         if ($vs_ids = trim($this->request->getParameter($t_subject->tableName(), pString))) {
             if ($vs_ids != 'all') {
                 $pa_ids = explode(';', $vs_ids);
                 foreach ($pa_ids as $vn_i => $vs_id) {
                     if (!trim($vs_id) || !(int) $vs_id) {
                         unset($pa_ids[$vn_i]);
                     }
                 }
             }
         }
         if (!is_array($pa_ids) || !sizeof($pa_ids)) {
             $pa_ids = $this->opo_result_context->getResultList();
         }
         $vn_file_count = 0;
         $o_view = new View($this->request, $this->request->getViewsDirectoryPath() . '/bundles/');
         if (is_array($pa_ids) && sizeof($pa_ids)) {
             $ps_version = $this->request->getParameter('version', pString);
             if ($qr_res = $t_subject->makeSearchResult($t_subject->tableName(), $pa_ids, array('filterNonPrimaryRepresentations' => false))) {
                 if (!($vn_limit = ini_get('max_execution_time'))) {
                     $vn_limit = 30;
                 }
                 set_time_limit($vn_limit * 10);
                 $o_zip = new ZipStream();
                 while ($qr_res->nextHit()) {
                     if (!is_array($va_version_list = $qr_res->getMediaVersions('ca_object_representations.media')) || !in_array($ps_version, $va_version_list)) {
                         $vs_version = 'original';
                     } else {
                         $vs_version = $ps_version;
                     }
                     $va_paths = $qr_res->getMediaPaths('ca_object_representations.media', $vs_version);
                     $va_infos = $qr_res->getMediaInfos('ca_object_representations.media');
                     $va_representation_ids = $qr_res->get('ca_object_representations.representation_id', array('returnAsArray' => true));
                     $va_representation_types = $qr_res->get('ca_object_representations.type_id', array('returnAsArray' => true));
                     foreach ($va_paths as $vn_i => $vs_path) {
                         $vs_ext = array_pop(explode(".", $vs_path));
                         $vs_idno_proc = preg_replace('![^A-Za-z0-9_\\-]+!', '_', $qr_res->get($t_subject->tableName() . '.idno'));
                         $vs_original_name = $va_infos[$vn_i]['ORIGINAL_FILENAME'];
                         $vn_index = sizeof($va_paths) > 1 ? "_" . ($vn_i + 1) : '';
                         $vn_representation_id = $va_representation_ids[$vn_i];
                         $vs_representation_type = caGetListItemIdno($va_representation_types[$vn_i]);
                         // make sure we don't download representations the user isn't allowed to read
                         if (!caCanRead($this->request->user->getPrimaryKey(), 'ca_object_representations', $vn_representation_id)) {
                             continue;
                         }
                         switch ($this->request->user->getPreference('downloaded_file_naming')) {
                             case 'idno':
                                 $vs_filename = "{$vs_idno_proc}{$vn_index}.{$vs_ext}";
                                 break;
                             case 'idno_and_version':
                                 $vs_filename = "{$vs_idno_proc}_{$vs_version}{$vn_index}.{$vs_ext}";
                                 break;
                             case 'idno_and_rep_id_and_version':
                                 $vs_filename = "{$vs_idno_proc}_representation_{$vn_representation_id}_{$vs_version}{$vn_index}.{$vs_ext}";
                                 break;
                             case 'original_name':
                             default:
                                 if ($vs_original_name) {
                                     $va_tmp = explode('.', $vs_original_name);
                                     if (sizeof($va_tmp) > 1) {
                                         if (strlen($vs_ext = array_pop($va_tmp)) < 3) {
                                             $va_tmp[] = $vs_ext;
                                         }
                                     }
                                     $vs_filename = join('_', $va_tmp) . "{$vn_index}.{$vs_ext}";
                                 } else {
                                     $vs_filename = "{$vs_idno_proc}_representation_{$vn_representation_id}_{$vs_version}{$vn_index}.{$vs_ext}";
                                 }
                                 break;
                         }
                         if ($o_media_metadata_conf->get('do_metadata_embedding_for_search_result_media_download')) {
                             if ($vs_path_with_embedding = caEmbedMediaMetadataIntoFile($vs_path, 'ca_objects', $qr_res->get('ca_objects.object_id'), caGetListItemIdno($qr_res->get('ca_objects.type_id')), $vn_representation_id, $vs_representation_type)) {
                                 $vs_path = $vs_path_with_embedding;
                             }
                         }
                         if (!file_exists($vs_path)) {
                             continue;
                         }
                         $o_zip->addFile($vs_path, $vs_filename);
                         $vn_file_count++;
                     }
                 }
             }
         }
         if ($o_zip && $vn_file_count > 0) {
             $o_view->setVar('zip_stream', $o_zip);
             $o_view->setVar('archive_name', 'media_for_' . mb_substr(preg_replace('![^A-Za-z0-9]+!u', '_', $this->getCriteriaForDisplay()), 0, 20) . '.zip');
             $this->response->addContent($o_view->render('download_file_binary.php'));
             set_time_limit($vn_limit);
             //$this->render('Results/object_representation_download_binary.php');
         } else {
             $this->response->setHTTPResponseCode(204, _t('No files to download'));
         }
         return;
     }
     // post error
     $this->postError(3100, _t("Could not generate ZIP file for download"), "BaseFindController->DownloadRepresentation()");
 }
 /**
  * Download all media attached to specified object (not necessarily open for editing)
  * Includes all representation media attached to the specified object + any media attached to other
  * objects in the same object hierarchy as the specified object. 
  */
 public function DownloadMedia($pa_options = null)
 {
     list($vn_subject_id, $t_subject) = $this->_initView();
     $pn_representation_id = $this->request->getParameter('representation_id', pInteger);
     $pn_value_id = $this->request->getParameter('value_id', pInteger);
     if ($pn_value_id) {
         return $this->DownloadAttributeFile();
     }
     $ps_version = $this->request->getParameter('version', pString);
     if (!$vn_subject_id) {
         return;
     }
     $o_view = new View($this->request, $this->request->getViewsDirectoryPath() . '/bundles/');
     if (!$ps_version) {
         $ps_version = 'original';
     }
     $o_view->setVar('version', $ps_version);
     $va_ancestor_ids = $t_subject->isHierarchical() ? $t_subject->getHierarchyAncestors(null, array('idsOnly' => true, 'includeSelf' => true)) : array($vn_subject_id);
     if ($vn_parent_id = array_pop($va_ancestor_ids)) {
         $t_subject->load($vn_parent_id);
         array_unshift($va_ancestor_ids, $vn_parent_id);
     }
     $va_child_ids = $t_subject->isHierarchical() ? $t_subject->getHierarchyChildren(null, array('idsOnly' => true)) : array($vn_subject_id);
     foreach ($va_ancestor_ids as $vn_id) {
         array_unshift($va_child_ids, $vn_id);
     }
     $vn_c = 1;
     $va_file_names = array();
     $va_file_paths = array();
     foreach ($va_child_ids as $vn_child_id) {
         if (!$t_subject->load($vn_child_id)) {
             continue;
         }
         if ($t_subject->tableName() == 'ca_object_representations') {
             $va_reps = array($vn_child_id => array('representation_id' => $vn_child_id, 'info' => array($ps_version => $t_subject->getMediaInfo('media', $ps_version))));
         } else {
             $va_reps = $t_subject->getRepresentations(array($ps_version));
         }
         $vs_idno = $t_subject->get('idno');
         foreach ($va_reps as $vn_representation_id => $va_rep) {
             if ($pn_representation_id && $pn_representation_id != $vn_representation_id) {
                 continue;
             }
             $va_rep_info = $va_rep['info'][$ps_version];
             $vs_idno_proc = preg_replace('![^A-Za-z0-9_\\-]+!', '_', $vs_idno);
             switch ($this->request->user->getPreference('downloaded_file_naming')) {
                 case 'idno':
                     $vs_file_name = $vs_idno_proc . '_' . $vn_c . '.' . $va_rep_info['EXTENSION'];
                     break;
                 case 'idno_and_version':
                     $vs_file_name = $vs_idno_proc . '_' . $ps_version . '_' . $vn_c . '.' . $va_rep_info['EXTENSION'];
                     break;
                 case 'idno_and_rep_id_and_version':
                     $vs_file_name = $vs_idno_proc . '_representation_' . $vn_representation_id . '_' . $ps_version . '.' . $va_rep_info['EXTENSION'];
                     break;
                 case 'original_name':
                 default:
                     if ($va_rep['info']['original_filename']) {
                         $va_tmp = explode('.', $va_rep['info']['original_filename']);
                         if (sizeof($va_tmp) > 1) {
                             if (strlen($vs_ext = array_pop($va_tmp)) < 3) {
                                 $va_tmp[] = $vs_ext;
                             }
                         }
                         $vs_file_name = join('_', $va_tmp);
                     } else {
                         $vs_file_name = $vs_idno_proc . '_representation_' . $vn_representation_id . '_' . $ps_version;
                     }
                     if (isset($va_file_names[$vs_file_name . '.' . $va_rep_info['EXTENSION']])) {
                         $vs_file_name .= "_{$vn_c}";
                     }
                     $vs_file_name .= '.' . $va_rep_info['EXTENSION'];
                     break;
             }
             $va_file_names[$vs_file_name] = true;
             $o_view->setVar('version_download_name', $vs_file_name);
             //
             // Perform metadata embedding
             $t_rep = new ca_object_representations($va_rep['representation_id']);
             if (!($vs_path = caEmbedMediaMetadataIntoFile($t_rep->getMediaPath('media', $ps_version), $t_subject->tableName(), $t_subject->getPrimaryKey(), $t_subject->getTypeCode(), $t_rep->getPrimaryKey(), $t_rep->getTypeCode()))) {
                 $vs_path = $t_rep->getMediaPath('media', $ps_version);
             }
             $va_file_paths[$vs_path] = $vs_file_name;
             $vn_c++;
         }
     }
     if (!($vn_limit = ini_get('max_execution_time'))) {
         $vn_limit = 30;
     }
     set_time_limit($vn_limit * 2);
     $o_zip = new ZipStream();
     foreach ($va_file_paths as $vs_path => $vs_name) {
         $o_zip->addFile($vs_path, $vs_name);
     }
     $o_view->setVar('zip_stream', $o_zip);
     $o_view->setVar('archive_name', preg_replace('![^A-Za-z0-9\\.\\-]+!', '_', $t_subject->get('idno')) . '.zip');
     $this->response->addContent($o_view->render('download_file_binary.php'));
     set_time_limit($vn_limit);
 }