root/trunk/system/libraries/Archive.php

Revision 2667, 3.2 kB (checked in by Geert, 2 months ago)

Follow-up to r2628. Using generic i18n messages for loading of drivers.

  • Property svn:eol-style set to LF
  • Property copyright set to Copyright (c) 2007 Kohana Team
  • Property svn:keywords set to Id
Line 
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3  * Archive library.
4  *
5  * $Id$
6  *
7  * @package    Archive
8  * @author     Kohana Team
9  * @copyright  (c) 2007-2008 Kohana Team
10  * @license    http://kohanaphp.com/license.html
11  */
12 class Archive_Core {
13
14     // Files and directories
15     protected $paths;
16
17     // Driver instance
18     protected $driver;
19
20     /**
21      * Loads the archive driver.
22      *
23      * @throws  Kohana_Exception
24      * @param   string   type of archive to create
25      * @return  void
26      */
27     public function __construct($type = NULL)
28     {
29         $type = empty($type) ? 'zip' : $type;
30
31         // Set driver name
32         $driver = 'Archive_'.ucfirst($type).'_Driver';
33
34         // Load the driver
35         if ( ! Kohana::auto_load($driver))
36             throw new Kohana_Exception('core.driver_not_found', $type, get_class($this));
37
38         // Initialize the driver
39         $this->driver = new $driver();
40
41         // Validate the driver
42         if ( ! ($this->driver instanceof Archive_Driver))
43             throw new Kohana_Exception('core.driver_implements', $type, get_class($this), 'Archive_Driver');
44
45         Log::add('debug', 'Archive Library initialized');
46     }
47
48     /**
49      * Adds files or directories, recursively, to an archive.
50      *
51      * @param   string   file or directory to add
52      * @param   string   name to use for the given file or directory
53      * @param   bool     add files recursively, used with directories
54      * @return  object
55      */
56     public function add($path, $name = NULL, $recursive = NULL)
57     {
58         // Normalize to forward slashes
59         $path = str_replace('\\', '/', $path);
60
61         // Set the name
62         empty($name) and $name = $path;
63
64         if (file_exists($path) AND is_dir($path))
65         {
66             // Force directories to end with a slash
67             $path = rtrim($path, '/').'/';
68             $name = rtrim($name, '/').'/';
69
70             // Add the directory to the paths
71             $this->paths[] = array($path, $name);
72
73             if ($recursive === TRUE)
74             {
75                 $dir = opendir($path);
76                 while (($file = readdir($dir)) !== FALSE)
77                 {
78                     // Do not add hidden files or directories
79                     if (substr($file, 0, 1) === '.')
80                         continue;
81
82                     // Add directory contents
83                     $this->add($path.$file, $name.$file, TRUE);
84                 }
85                 closedir($dir);
86             }
87         }
88         else
89         {
90             $this->paths[] = array($path, $name);
91         }
92
93         return $this;
94     }
95
96     /**
97      * Creates an archive and saves it into a file.
98      *
99      * @throws  Kohana_Exception
100      * @param   string   archive filename
101      * @return  boolean
102      */
103     public function save($filename)
104     {
105         // Get the directory name
106         $directory = pathinfo($filename, PATHINFO_DIRNAME);
107
108         if ( ! is_writable($directory))
109             throw new Kohana_Exception('archive.directory_unwritable', $directory);
110
111         if (file_exists($filename))
112         {
113             // Unable to write to the file
114             if ( ! is_writable($filename))
115                 throw new Kohana_Exception('archive.filename_conflict', $filename);
116
117             // Remove the file
118             unlink($filename);
119         }
120
121         return $this->driver->create($this->paths, $filename);
122     }
123
124     /**
125      * Creates a raw archive file and returns it.
126      *
127      * @return  string
128      */
129     public function create()
130     {
131         return $this->driver->create($this->paths);
132     }
133
134     /**
135      * Forces a download of a created archive.
136      *
137      * @param   string   name of the file that will be downloaded
138      * @return  void
139      */
140     public function download($filename)
141     {
142         download::force($filename, $this->driver->create($this->paths));
143     }
144
145 } // End Archive
Note: See TracBrowser for help on using the browser.