Changeset 1924

Show
Ignore:
Timestamp:
02/05/2008 10:39:20 AM (11 months ago)
Author:
Shadowhand
Message:

Finally fixing the Zip Archive problem. And I mean it this time.

Location:
trunk/system/libraries
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/Archive.php

    r1911 r1924  
    4949         * Adds files or directories, recursively, to an archive. 
    5050         * 
    51          * @param   string       file or directory to add 
    52          * @param   string|bool 
     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 
    5354         * @return  object 
    5455         */ 
    55         public function add($path, $name = TRUE) 
     56        public function add($path, $name = NULL, $recursive = NULL) 
    5657        { 
    5758                // Normalize to forward slashes 
    5859                $path = str_replace('\\', '/', $path); 
    5960 
    60                 // Enable or disable recursion 
    61                 $recursive = ($name === TRUE) ? TRUE : FALSE; 
    62  
    6361                // Set the name 
    64                 $name = is_string($name) ? $name : $path; 
     62                ($name === NULL) and $name = $path; 
    6563 
    6664                if (file_exists($path) AND is_dir($path)) 
    6765                { 
     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 
    6873                        if ($recursive == TRUE) 
    6974                        { 
    70                                 // Force directories to end with a slash 
    71                                 $path = rtrim($path, '/').'/'; 
    72  
    7375                                $dir = opendir($path); 
    7476                                while (($file = readdir($dir)) !== FALSE) 
     
    7981 
    8082                                        // Add directory contents 
    81                                         $this->add($path.$file); 
     83                                        $this->add($path.$file, $name.$file, TRUE); 
    8284                                } 
    8385                                closedir($dir); 
  • trunk/system/libraries/drivers/Archive.php

    r1911 r1924  
    2121        public function create($paths, $filename = FALSE); 
    2222 
     23        /** 
     24         * Add data to the archive. 
     25         * 
     26         * @param   string   filename 
     27         * @param   string   name of file in archive 
     28         * @return  void 
     29         */ 
     30        public function add_data($file, $name, $contents = NULL); 
     31 
    2332} // End Archive_Driver Interface 
  • trunk/system/libraries/drivers/Archive/Zip.php

    r1911 r1924  
    2828                foreach ($paths as $set) 
    2929                { 
    30                         // Add each file 
    31                         $this->add_file($set[0], $set[1]); 
     30                        // Add each path individually 
     31                        $this->add_data($set[0], $set[1], isset($set[2]) ? $set[2] : NULL); 
    3232                } 
    3333 
     
    7777        } 
    7878 
    79         /** 
    80          * Adds a directory to a zip. 
    81          * 
    82          * @param  string  path to directory 
    83          * @param  string  name of directory 
    84          */ 
    85         protected function add_dir($dir, $name) 
     79        public function add_data($file, $name, $contents = NULL) 
    8680        { 
    87                 // Fetch the timestamp 
    88                 $timestamp = date::unix2dos(filemtime($dir)); 
     81                // Determine the file type: 16 = dir, 32 = file 
     82                $type = (substr($file, -1) === '/') ? 16 : 32; 
    8983 
    90                 $this->data[] = 
    91                         // Start "local file header" 
    92                         "\x50\x4b\x03\x04".       // Zip header 
    93                         "\x0a\x00".               // Version required for extraction 
    94                         "\x00\x00".               // General bit flag 
    95                         "\x00\x00".               // Compression method 
    96                         pack('V', $timestamp).    // Last mod time and date 
    97                         pack('V', crc32($name)).  // CRC32 
    98                         pack('V', 0).             // Compressed filesize 
    99                         pack('V', 0).             // Uncompressed filesize 
    100                         pack('v', strlen($name)). // Length of directory name 
    101                         pack('v', 0).             // Extra field length 
    102                         $name;                    // Directory name 
     84                // Fetch the timestamp, using the current time if manually setting the contents 
     85                $timestamp = date::unix2dos(($contents === NULL) ? filemtime($file) : time()); 
    10386 
    104                 $this->dirs[] = 
    105                         "\x50\x4b\x01\x02".       // Zip header 
    106                         "\x00\x00".               // Version made by 
    107                         "\x0a\x00".               // Version required for extraction 
    108                         "\x00\x00".               // General bit flag 
    109                         "\x00\x00".               // Compression method 
    110                         pack('V', $timestamp).    // Last mod time and date 
    111                         pack('V', crc32($name)).  // CRC32 
    112                         pack('V', 0).             // Compressed filesize 
    113                         pack('V', 0).             // Uncompressed filesize 
    114                         pack('v', strlen($name)). // Length of directory name 
    115                         pack('v', 0).             // Extra field length 
    116                         // Data description 
    117                         pack('v', 0).             // CRC32 
    118                         pack('v', 0).             // Compressed filesize 
    119                         pack('v', 0).             // Uncompressed filesize 
    120                         pack('V', 16).            // Internal file attribute "directory" 
    121                         pack('V', $this->offset). // Directory offset 
    122                         $name;                    // Directory name 
    123  
    124                 // Set the new offset 
    125                 $this->offset = strlen(implode('', $this->data)); 
    126         } 
    127  
    128         /** 
    129          * Adds a file to a zip. 
    130          * 
    131          * @param  string  path to file 
    132          * @param  string  name of file 
    133          */ 
    134         protected function add_file($file, $name) 
    135         { 
    136                 // Fetch the timestamp 
    137                 $timestamp = date::unix2dos(filemtime($file)); 
    138  
    139                 // Read the file 
    140                 $data = file_get_contents($file); 
     87                // Read the file or use the defined contents 
     88                $data = ($contents === NULL) ? file_get_contents($file) : $contents; 
    14189 
    14290                // Gzip the data, use substr to fix a CRC bug 
     
    174122                        pack('v', 0).             // Compressed filesize 
    175123                        pack('v', 0).             // Uncompressed filesize 
    176                         pack('V', 32).            // External file attribute "file" 
     124                        pack('V', $type).         // File attribute type 
    177125                        pack('V', $this->offset). // Directory offset 
    178126                        $name;                    // File name