1.20.x common.inc archiver_get_archiver($file)

Creates the appropriate archiver for the specified file.

Parameters

$file: The full path of the archive file. Note that stream wrapper paths are supported, but not remote ones.

Return value

ArchiverInterface: A newly created instance of the archiver class appropriate for the specified file, already bound to that file. If no appropriate archiver class was found, will return FALSE.

Throws

Exception

File

includes/common.inc, line 8608
Common functions that many Backdrop modules will need to reference.

Code

function archiver_get_archiver($file) {
  // Archivers can only work on local paths
  $filepath = backdrop_realpath($file);
  if (!is_file($filepath)) {
    throw new Exception(t('Archivers can only operate on local files: %file not supported', array('%file' => $file)));
  }
  $archiver_info = archiver_get_info();

  foreach ($archiver_info as $implementation) {
    foreach ($implementation['extensions'] as $extension) {
      // Because extensions may be multi-part, such as .tar.gz,
      // we cannot use simpler approaches like substr() or pathinfo().
      // This method isn't quite as clean but gets the job done.
      // Also note that the file may not yet exist, so we cannot rely
      // on fileinfo() or other disk-level utilities.
      if (strrpos($filepath, '.' . $extension) === strlen($filepath) - strlen('.' . $extension)) {
        return new $implementation['class']($filepath);
      }
    }
  }

  // No valid archiver found.
  return FALSE;
}