You are here

function DrupalGapZip in DrupalGap 7

Same name and namespace in other branches
  1. 7.2 drupalgap.module \DrupalGapZip()

See also

http://stackoverflow.com/a/1334949/763010

1 call to DrupalGapZip()
drupalgap_download_app in ./drupalgap.pages.inc
Zip's up the "mobile-application" directory and downloads it for the user.

File

./drupalgap.module, line 689
A module to provide a bridge between Drupal websites and PhoneGap mobile applications.

Code

function DrupalGapZip($source, $destination) {

  // credits: http://stackoverflow.com/users/89771/alix-axel
  if (!extension_loaded('zip') || !file_exists($source)) {
    return false;
  }
  $zip = new ZipArchive();
  if (!$zip
    ->open($destination, ZIPARCHIVE::CREATE)) {
    return false;
  }
  $source = str_replace('\\', '/', realpath($source));
  if (is_dir($source) === true) {
    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
    foreach ($files as $file) {
      $file = str_replace('\\', '/', $file);

      // Ignore "." and ".." folders
      if (in_array(substr($file, strrpos($file, '/') + 1), array(
        '.',
        '..',
      ))) {
        continue;
      }
      $file = realpath($file);
      if (is_dir($file) === true) {
        $zip
          ->addEmptyDir(str_replace($source . '/', '', $file . '/'));
      }
      else {
        if (is_file($file) === true) {
          $zip
            ->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
        }
      }
    }
  }
  else {
    if (is_file($source) === true) {
      $zip
        ->addFromString(basename($source), file_get_contents($source));
    }
  }
  return $zip
    ->close();
}