Blame view

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