示例#1
0
 /**
  * Create a writer that can be used to append files to an archive inside a source
  * If the archive can't be found in the source, it will be created
  * If source is set to null, File_Archive::toFiles will be assumed
  * If type is set to null, the type of the archive will be determined looking at
  * the extension in the URL
  * stat is the array of stat (returned by stat() PHP function of Reader getStat())
  * to use if the archive must be created
  *
  * This function allows to create or append data to nested archives. Only one
  * archive will be created and if your creation requires creating several nested
  * archives, a PEAR error will be returned
  *
  * After this call, $source will be closed and should not be used until the
  * returned writer is closed.
  *
  * @param File_Archive_Reader $source A reader where some files will be appended
  * @param string $URL URL to reach the archive in the source.
  *        if $URL is null, a writer to append files to the $source reader will
  *        be returned
  * @param bool $unique If true, the duplicate files will be deleted on close
  *        Default is false (and setting it to true may have some performance
  *        consequences)
  * @param string $type Extension of the archive (or null to use the one in the URL)
  * @param array $stat Used only if archive is created, array of stat as returned
  *        by PHP stat function or Reader getStat function: stats of the archive)
  *        Time (index 9) will be overwritten to current time
  * @return File_Archive_Writer a writer that you can use to append files to the reader
  */
 function appenderFromSource(&$toConvert, $URL = null, $unique = null, $type = null, $stat = array())
 {
     $source =& File_Archive::_convertToReader($toConvert);
     if (PEAR::isError($source)) {
         return $source;
     }
     if ($unique == null) {
         $unique = File_Archive::getOption("appendRemoveDuplicates");
     }
     //Do not report the fact that the archive does not exist as an error
     PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
     if ($URL === null) {
         $result =& $source;
     } else {
         if ($type === null) {
             $result = File_Archive::_readSource($source, $URL . '/', $reachable, $baseDir);
         } else {
             $result = File_Archive::readArchive($type, File_Archive::_readSource($source, $URL, $reachable, $baseDir));
         }
     }
     PEAR::popErrorHandling();
     if (!PEAR::isError($result)) {
         if ($unique) {
             require_once dirname(__FILE__) . "/Archive/Writer/UniqueAppender.php";
             return new File_Archive_Writer_UniqueAppender($result);
         } else {
             return $result->makeAppendWriter();
         }
     }
     //The source can't be found and has to be created
     $stat[9] = $stat['mtime'] = time();
     if (empty($baseDir)) {
         if ($source !== null) {
             $writer =& $source->makeWriter();
         } else {
             $writer =& File_Archive::toFiles();
         }
         if (PEAR::isError($writer)) {
             return $writer;
         }
         PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
         $result = File_Archive::toArchive($reachable, $writer, $type);
         PEAR::popErrorHandling();
         if (PEAR::isError($result)) {
             $result = File_Archive::toFiles($reachable);
         }
     } else {
         $reachedSource = File_Archive::readSource($source, $reachable);
         if (PEAR::isError($reachedSource)) {
             return $reachedSource;
         }
         $writer = $reachedSource->makeWriter();
         if (PEAR::isError($writer)) {
             return $writer;
         }
         PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
         $result = File_Archive::toArchive($baseDir, $writer, $type);
         PEAR::popErrorHandling();
         if (PEAR::isError($result)) {
             require_once dirname(__FILE__) . "/Archive/Writer/AddBaseName.php";
             $result = new File_Archive_Writer_AddBaseName($baseDir, $writer);
             if (PEAR::isError($result)) {
                 return $result;
             }
         }
     }
     return $result;
 }