📄 archive.php
字号:
*
* @param bool $sendHeaders If true some headers will be sent to force the
* download of the file. Default value is true
* @see File_Archive_Writer_Output
*/
function toOutput($sendHeaders = true)
{
require_once PEAR_DIR."File/Archive/Writer/Output.php";
return new File_Archive_Writer_Output($sendHeaders);
}
/**
* Compress the data to a tar, gz, tar/gz or zip format
*
* @param string $filename name of the archive file
* @param File_Archive_Writer $innerWriter writer where the archive will be
* written
* @param string $type can be one of tgz, tbz, tar, zip, gz, gzip, bz2,
* bzip2 (default is the extension of $filename) or any composition
* of them (for example tar.gz or tar.bz2). The case of this
* parameter is not important.
* @param array $stat Statistics of the archive (see stat function)
* @param bool $autoClose If set to true, $innerWriter will be closed when
* the returned archive is close. Default value is true.
*/
function toArchive($filename, &$toConvert, $type = null,
$stat = array(), $autoClose = true)
{
$innerWriter =& File_Archive::_convertToWriter($toConvert);
if (PEAR::isError($innerWriter)) {
return $innerWriter;
}
$shortcuts = array("tgz" , "tbz" );
$reals = array("tar.gz", "tar.bz2");
if ($type === null) {
$extensions = strtolower($filename);
} else {
$extensions = strtolower($type);
}
$extensions = explode('.', str_replace($shortcuts, $reals, $extensions));
if ($innerWriter !== null) {
$writer =& $innerWriter;
} else {
$writer = File_Archive::toFiles();
}
$nbCompressions = 0;
$currentFilename = $filename;
while (($extension = array_pop($extensions)) !== null) {
unset($next);
switch($extension) {
case "tar":
require_once PEAR_DIR."File/Archive/Writer/Tar.php";
$next = new File_Archive_Writer_Tar(
$currentFilename, $writer, $stat, $autoClose
);
unset($writer); $writer =& $next;
break;
case "zip":
require_once PEAR_DIR."File/Archive/Writer/Zip.php";
$next = new File_Archive_Writer_Zip(
$currentFilename, $writer, $stat, $autoClose
);
unset($writer); $writer =& $next;
break;
case "gz":
case "gzip":
require_once PEAR_DIR."File/Archive/Writer/Gzip.php";
$next = new File_Archive_Writer_Gzip(
$currentFilename, $writer, $stat, $autoClose
);
unset($writer); $writer =& $next;
break;
case "bz2":
case "bzip2":
require_once PEAR_DIR."File/Archive/Writer/Bzip2.php";
$next = new File_Archive_Writer_Bzip2(
$currentFilename, $writer, $stat, $autoClose
);
unset($writer); $writer =& $next;
break;
case "deb":
case "ar":
require_once PEAR_DIR."File/Archive/Writer/Ar.php";
$next = new File_Archive_Writer_Ar(
$currentFilename, $writer, $stat, $autoClose
);
unset($writer); $writer =& $next;
break;
default:
if ($type !== null || $nbCompressions == 0) {
return PEAR::raiseError("Archive $extension unknown");
}
break;
}
$nbCompressions ++;
$autoClose = true;
$currentFilename = implode(".", $extensions);
}
return $writer;
}
/**
* File_Archive::extract($source, $dest) is equivalent to $source->extract($dest)
* If $source is a PEAR error, the error will be returned
* It is thus easier to use this function than $source->extract, since it reduces the number of
* error checking and doesn't force you to define a variable $source
*
* You may use strings as source and dest. In that case the source is automatically
* converted to a reader using File_Archive::read and the dest is converted to a
* writer using File_Archive::appender
* Since PHP doesn't allow to pass literal strings by ref, you will have to use temporary
* variables.
* File_Archive::extract($src = 'archive.zip/', $dest = 'dir') will extract the archive to 'dir'
* It is the same as
* File_Archive::extract(
* File_Archive::read('archive.zip/'),
* File_Archive::appender('dir')
* );
* You may use any variable in the extract function ($from/$to, $a/$b...).
*
* @param File_Archive_Reader $source The source that will be read
* @param File_Archive_Writer $dest Where to copy $source files
* @param bool $autoClose if true (default), $dest will be closed after the extraction
* @param int $bufferSize Size of the buffer to use to move data from the reader to the buffer
* If $bufferSize <= 0 (default), the blockSize option is used
* You shouldn't need to change that
* @return null or a PEAR error if an error occured
*/
function extract(&$sourceToConvert, &$destToConvert, $autoClose = true, $bufferSize = 0)
{
$source =& File_Archive::_convertToReader($sourceToConvert);
if (PEAR::isError($source)) {
return $source;
}
$dest =& File_Archive::_convertToWriter($destToConvert);
return $source->extract($dest, $autoClose, $bufferSize);
}
/**
* 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 PEAR_DIR."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 PEAR_DIR."File/Archive/Writer/AddBaseName.php";
$result = new File_Archive_Writer_AddBaseName(
$baseDir, $writer);
if (PEAR::isError($result)) {
return $result;
}
}
}
return $result;
}
/**
* Create a writer that allows appending new files to an existing archive
* This function actes as appendToSource with source being the system files
* $URL can't be null here
*
* @param File_Archive_Reader $source A reader where some files will be appended
* @return File_Archive_Writer a writer that you can use to append files to the reader
*/
function appender($URL, $unique = null, $type = null, $stat = array())
{
$source = null;
return File_Archive::appenderFromSource($source, $URL, $unique, $type, $stat);
}
/**
* Remove the files that follow a given predicate from the source
* If URL is null, the files will be removed from the source directly
* Else, URL must link to a source from which the files will be removed
*
* @param File_Archive_Predicate $pred The files that follow the predicate
* (for which $pred->isTrue($source) is true) will be erased
* @param File_Archive_Reader $source A reader that contains the files to remove
*/
function removeFromSource(&$pred, &$toConvert, $URL = null)
{
$source =& File_Archive::_convertToReader($toConvert);
if (PEAR::isError($source)) {
return $source;
}
if ($URL === null) {
$result = &$source;
} else {
if (substr($URL, -1) !== '/') {
$URL .= '/';
}
$result = File_Archive::readSource($source, $URL);
}
$writer = $result->makeWriterRemoveFiles($pred);
if (PEAR::isError($writer)) {
return $writer;
}
$writer->close();
}
/**
* Remove the files that follow a given predicate from the archive specified
* in $URL
*
* @param $URL URL of the archive where some files must be removed
*/
function remove($pred, $URL)
{
$source = null;
return File_Archive::removeFromSource($pred, $source, $URL);
}
/**
* Remove duplicates from a source, keeping the most recent one (or the one that has highest pos in
* the archive if the files have same date or no date specified)
*
* @param File_Archive_Reader a reader that may contain duplicates
*/
function removeDuplicatesFromSource(&$toConvert, $URL = null)
{
$source =& File_Archive::_convertToReader($toConvert);
if (PEAR::isError($source)) {
return $source;
}
if ($URL !== null && substr($URL, -1) != '/') {
$URL .= '/';
}
if ($source === null) {
$source = File_Archive::read($URL);
}
require_once PEAR_DIR."File/Archive/Predicate/Duplicate.php";
$pred = new File_Archive_Predicate_Duplicate($source);
$source->close();
return File_Archive::removeFromSource(
$pred,
$source,
null
);
}
/**
* Remove duplicates from the archive specified in the URL
*/
function removeDuplicates($URL)
{
$source = null;
return File_Archive::removeDuplicatesFromSource($source, $URL);
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -