You are here

function zip_files in Quiz 6.6

Same name and namespace in other branches
  1. 6.5 includes/moodle_support.php \zip_files()

Zip an array of files/dirs to a destination zip file Both parameters must be FULL paths to the files/dirs

1 call to zip_files()
qformat_qti2::exportprocess in includes/moodle/question/format/qti2/format.php
exports the questions in a question category to the given location

File

includes/moodle_support.php, line 439

Code

function zip_files($originalfiles, $destination) {
  global $CFG;
  print "zipping files! originalfiles: ";
  dprint_r($originalfiles);
  print "destination: {$destination}\n";

  //Extract everything from destination
  $path_parts = pathinfo(cleardoubleslashes($destination));
  $destpath = $path_parts["dirname"];

  //The path of the zip file
  $destfilename = $path_parts["basename"];

  //The name of the zip file
  $extension = $path_parts["extension"];

  //The extension of the file
  foreach ($originalfiles as $file) {

    //Iterate over each file

    //Check for every file
    $tempfile = cleardoubleslashes($file);

    // no doubleslashes!

    //Calculate the base path for all files if it isn't set
    if ($origpath === NULL) {
      $origpath = rtrim(cleardoubleslashes(dirname($tempfile)), "/");
    }

    //See if the file is readable
    if (!is_readable($tempfile)) {

      //Is readable
      continue;
    }

    //See if the file/dir is in the same directory than the rest
    if (rtrim(cleardoubleslashes(dirname($tempfile)), "/") != $origpath) {
      continue;
    }

    //Add the file to the array
    $files[] = $tempfile;
  }
  if (class_exists('ZipArchive')) {
    $zip = new ZipArchive();
    $filename = $destination;

    // correct?
    if ($zip
      ->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
      exit("cannot open <{$filename}>\n");
    }
    foreach ($files as $file) {
      $nameInZip = basename($originalFile);
      $zip
        ->addFile($originalFile, $nameInZip);
    }
    echo "numfiles: " . $zip->numFiles . "\n";
    echo "status:" . $zip->status . "\n";
    $zip
      ->close();
  }
  else {
    if (function_exists('gzopen')) {
      require_once "{$CFG->libdir}/pclzip/pclzip.lib.php";

      //rewrite filenames because the old method with PCLZIP_OPT_REMOVE_PATH does not work under win32
      $zipfiles = array();
      $start = strlen($origpath) + 1;
      foreach ($files as $file) {
        $tf = array();
        $tf[PCLZIP_ATT_FILE_NAME] = $file;
        $tf[PCLZIP_ATT_FILE_NEW_FULL_NAME] = substr($file, $start);
        $zipfiles[] = $tf;
      }

      //create the archive
      $archive = new PclZip(cleardoubleslashes("{$destpath}/{$destfilename}"));
      if ($list = $archive
        ->create($zipfiles) == 0) {
        notice($archive
          ->errorInfo(true));
        return false;
      }
    }
    else {
      print "final else";
      $filestozip = "";
      foreach ($files as $filetozip) {
        $filestozip .= escapeshellarg(basename($filetozip));
        $filestozip .= " ";
      }

      //Construct the command
      $separator = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? ' &' : ' ;';
      $command = 'cd ' . escapeshellarg($origpath) . $separator . escapeshellarg($CFG->zip) . ' -r ' . escapeshellarg(cleardoubleslashes("{$destfilename}")) . ' ' . $filestozip;

      //All converted to backslashes in WIN
      if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        $command = str_replace('/', '\\', $command);
      }

      // print "$command";exit;
      Exec($command);
    }
  }
}