/** * @see File_Archive_Reader::next() */ function next() { if (!parent::next()) { return false; } $this->nbRead++; $this->filePos = 0; if ($this->nbRead > 1) { return false; } $dataFilename = $this->source->getDataFilename(); if ($dataFilename !== null) { $this->tmpName = null; $this->gzfile = gzopen($dataFilename, 'r'); } else { $this->tmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far'); //Generate the tmp data $dest = new File_Archive_Writer_Files(); $dest->newFile($this->tmpName); $this->source->sendData($dest); $dest->close(); $this->gzfile = gzopen($this->tmpName, 'r'); } return true; }
/** * @see File_Archive_Reader::next() */ function next() { if (!parent::next()) { return false; } $this->nbRead++; if ($this->nbRead > 1) { return false; } $dataFilename = $this->source->getDataFilename(); if ($dataFilename !== null) { $this->tmpName = null; $this->bzfile = @bzopen($dataFilename, 'r'); if ($this->bzfile === false) { return PEAR::raiseError("bzopen failed to open {$dataFilename}"); } } else { $this->tmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far'); //Generate the tmp data $dest = new File_Archive_Writer_Files(); $dest->newFile($this->tmpName); $this->source->sendData($dest); $dest->close(); $this->bzfile = bzopen($this->tmpName, 'r'); } return true; }
/** * @see File_Archive_Writer::newFile() * * Check that one single file is written in the GZip archive */ function newFile($filename, $stat = array(), $mime = "application/octet-stream") { if ($this->nbFiles > 1) { return PEAR::raiseError("A Gz archive can only contain one single file." . "Use Tgz archive to be able to write several files"); } $this->nbFiles++; $this->tmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far'); $this->gzfile = gzopen($this->tmpName, 'w' . $this->compressionLevel); return true; }
/** * @see File_Archive_Reader::getData() */ function getData($length = -1) { if ($this->fromSource) { $data = $this->source->getData($length); if (PEAR::isError($data)) { return $data; } fwrite($this->tmpFile, $data); return $data; } else { if ($length == 0) { return ''; } if ($length > 0 && $this->pos + 1 < count($this->files)) { $maxSize = $this->files[$this->pos + 1]['pos'] - ftell($this->tmpFile); if ($maxSize == 0) { return null; } if ($length > $maxSize) { $length = $maxSize; } return fread($this->tmpFile, $length); } else { $contents = ''; $blockSize = File_Archive::getOption('blockSize'); while (!feof($this->tmpFile)) { $contents .= fread($this->tmpFile, $blockSize); } return $contents == '' ? null : $contents; } } }
/** * Sends the current file to the Writer $writer * The data will be sent by chunks of at most $bufferSize bytes * If $bufferSize <= 0 (default), the blockSize option is used */ function sendData(&$writer, $bufferSize = 0) { if (PEAR::isError($writer)) { return $writer; } if ($bufferSize <= 0) { $bufferSize = File_Archive::getOption('blockSize'); } $filename = $this->getDataFilename(); if ($filename !== null) { $error = $writer->writeFile($filename); if (PEAR::isError($error)) { return $error; } } else { while (($data = $this->getData($bufferSize)) !== null) { if (PEAR::isError($data)) { return $data; } $error = $writer->writeData($data); if (PEAR::isError($error)) { return $error; } } } }
function appendFile($filename, $dataFilename) { //Try to read from the cache $cache = File_Archive::getOption('cache', null); if ($cache !== null && $this->compressionLevel > 0) { $id = realpath($dataFilename); $id = urlencode($id); $id = str_replace('_', '%5F', $id); $group = 'FileArchiveZip' . $this->compressionLevel; $mtime = filemtime($dataFilename); //Tries to read from cache if (($data = $cache->get($id, $group)) !== false) { $info = unpack('Vmtime/Vcrc/Vnlength', substr($data, 0, 12)); $data = substr($data, 12); } //If cache failed or file modified since then if ($data === false || $info['mtime'] != $mtime) { $data = file_get_contents($dataFilename); $info = array('crc' => crc32($data), 'nlength' => strlen($data), 'mtime' => $mtime); $data = gzcompress($data, $this->compressionLevel); $data = substr($data, 2, strlen($data) - 6); $data = pack('VVV', $info['mtime'], $info['crc'], $info['nlength']) . $data; $cache->save($data, $id, $group); } return $this->appendCompressedData($filename, stat($dataFilename), $data, $info['crc'], $info['nlength']); } //If no cache system, use the standard way return parent::appendFile($filename, $dataFilename); }
/** * 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; }
/** * @see File_Archive_Reader::getData() */ function getData($length = -1) { $error = $this->_ensureFileOpened(); if (PEAR::isError($error)) { return $error; } if (feof($this->handle)) { return null; } if ($length == -1) { $contents = ''; $blockSize = File_Archive::getOption('blockSize'); while (!feof($this->handle)) { $contents .= fread($this->handle, $blockSize); } return $contents; } else { if ($length == 0) { return ""; } else { return fread($this->handle, $length); } } }
/** * Ensure data has been extracted to $this->entryTmpName */ function ensureDataExtracted() { if ($this->fileReader !== null) { return; } if ($this->entryTmpName === null) { $this->entryTmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far'); } $this->rarEntry->extract(false, $this->entryTmpName); $this->fileReader = new File_Archive_Reader_File($this->entryTmpName, $this->rarEntry->getName()); }
/** * @see File_Archive_Reader::getData() */ function getData($length = -1) { $error = $this->_ensureFileOpened(); if (PEAR::isError($error)) { return $error; } if (feof($this->handle)) { return null; } if ($length == -1) { $contents = ''; $blockSize = File_Archive::getOption('blockSize'); // Ensure that magic_quote_runtime isn't set, // if we don't want to have corrupted archives. $saveMagicQuotes = get_magic_quotes_runtime(); @set_magic_quotes_runtime(0); while (!feof($this->handle)) { $contents .= fread($this->handle, $blockSize); } @set_magic_quotes_runtime($saveMagicQuotes); return $contents; } else { if ($length == 0) { return ""; } else { return fread($this->handle, $length); } } }