home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTTP / Download / Archive.php next >
Encoding:
PHP Script  |  2008-07-02  |  3.5 KB  |  123 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * HTTP::Download::Archive
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * @category   HTTP
  10.  * @package    HTTP_Download
  11.  * @author     Michael Wallner <mike@php.net>
  12.  * @copyright  2003-2005 Michael Wallner
  13.  * @license    BSD, revisewd
  14.  * @version    CVS: $Id: Archive.php,v 1.4 2005/11/13 19:18:55 mike Exp $
  15.  * @link       http://pear.php.net/package/HTTP_Download
  16.  */
  17.  
  18. /**
  19.  * Requires HTTP_Download
  20.  */
  21. require_once 'HTTP/Download.php';
  22.  
  23. /**
  24.  * Requires System
  25.  */
  26. require_once 'System.php';
  27.  
  28. /** 
  29.  * HTTP_Download_Archive
  30.  * 
  31.  * Helper class for sending Archives.
  32.  *
  33.  * @access   public
  34.  * @version  $Revision: 1.4 $
  35.  */
  36. class HTTP_Download_Archive
  37. {
  38.     /**
  39.      * Send a bunch of files or directories as an archive
  40.      * 
  41.      * Example:
  42.      * <code>
  43.      *  require_once 'HTTP/Download/Archive.php';
  44.      *  HTTP_Download_Archive::send(
  45.      *      'myArchive.tgz',
  46.      *      '/var/ftp/pub/mike',
  47.      *      HTTP_DOWNLOAD_BZ2,
  48.      *      '',
  49.      *      '/var/ftp/pub'
  50.      *  );
  51.      * </code>
  52.      *
  53.      * @see         Archive_Tar::createModify()
  54.      * @static
  55.      * @access  public
  56.      * @return  mixed   Returns true on success or PEAR_Error on failure.
  57.      * @param   string  $name       name the sent archive should have
  58.      * @param   mixed   $files      files/directories
  59.      * @param   string  $type       archive type
  60.      * @param   string  $add_path   path that should be prepended to the files
  61.      * @param   string  $strip_path path that should be stripped from the files
  62.      */
  63.     function send($name, $files, $type = HTTP_DOWNLOAD_TGZ, $add_path = '', $strip_path = '')
  64.     {
  65.         $tmp = System::mktemp();
  66.         
  67.         switch ($type = strToUpper($type))
  68.         {
  69.             case HTTP_DOWNLOAD_TAR:
  70.                 include_once 'Archive/Tar.php';
  71.                 $arc = &new Archive_Tar($tmp);
  72.                 $content_type = 'x-tar';
  73.             break;
  74.  
  75.             case HTTP_DOWNLOAD_TGZ:
  76.                 include_once 'Archive/Tar.php';
  77.                 $arc = &new Archive_Tar($tmp, 'gz');
  78.                 $content_type = 'x-gzip';
  79.             break;
  80.  
  81.             case HTTP_DOWNLOAD_BZ2:
  82.                 include_once 'Archive/Tar.php';
  83.                 $arc = &new Archive_Tar($tmp, 'bz2');
  84.                 $content_type = 'x-bzip2';
  85.             break;
  86.  
  87.             case HTTP_DOWNLOAD_ZIP:
  88.                 include_once 'Archive/Zip.php';
  89.                 $arc = &new Archive_Zip($tmp);
  90.                 $content_type = 'x-zip';
  91.             break;
  92.             
  93.             default:
  94.                 return PEAR::raiseError(
  95.                     'Archive type not supported: ' . $type,
  96.                     HTTP_DOWNLOAD_E_INVALID_ARCHIVE_TYPE
  97.                 );
  98.         }
  99.         
  100.         if ($type == HTTP_DOWNLOAD_ZIP) {
  101.             $options = array(   'add_path' => $add_path, 
  102.                                 'remove_path' => $strip_path);
  103.             if (!$arc->create($files, $options)) {
  104.                 return PEAR::raiseError('Archive creation failed.');
  105.             }
  106.         } else {
  107.             if (!$e = $arc->createModify($files, $add_path, $strip_path)) {
  108.                 return PEAR::raiseError('Archive creation failed.');
  109.             }
  110.             if (PEAR::isError($e)) {
  111.                 return $e;
  112.             }
  113.         }
  114.         unset($arc);
  115.         
  116.         $dl = &new HTTP_Download(array('file' => $tmp));
  117.         $dl->setContentType('application/' . $content_type);
  118.         $dl->setContentDisposition(HTTP_DOWNLOAD_ATTACHMENT, $name);
  119.         return $dl->send();
  120.     }
  121. }
  122. ?>
  123.