cvs: pear /File_Archive/Archive Reader.php /File_Archive/Archive/Writer Tar.php /File_Archive/tests test.php
| From: | Vincent Lascaux | Date: | Sat, 16 Apr 2005 08:59:20 +0000 |
| Subject: | cvs: pear /File_Archive/Archive Reader.php /File_Archive/Archive/Writer Tar.php /File_Archive/tests test.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-31194@lists.php.net to get a copy of this message | ||
vincentlascaux Sat Apr 16 04:59:20 2005 EDT
Modified files:
/pear/File_Archive/Archive Reader.php
/pear/File_Archive/Archive/Writer Tar.php
/pear/File_Archive/tests test.php
Log:
+ Remove the constraint on Reader::getStat(): index 7 (size) don't have to be present (but
it's still better if it is)
+ Tar writer takes less memory for not physical files (for example read from another archive) with
a known size
http://cvs.php.net/diff.php/pear/File_Archive/Archive/Reader.php?r1=1.13&r2=1.14&ty=u
Index: pear/File_Archive/Archive/Reader.php
diff -u pear/File_Archive/Archive/Reader.php:1.13 pear/File_Archive/Archive/Reader.php:1.14
--- pear/File_Archive/Archive/Reader.php:1.13 Fri Feb 25 19:15:41 2005
+++ pear/File_Archive/Archive/Reader.php Sat Apr 16 04:59:15 2005
@@ -27,7 +27,7 @@
* @author Vincent Lascaux <vincentlascaux@php.net>
* @copyright 1997-2005 The PHP Group
* @license http://www.gnu.org/copyleft/lesser.html LGPL
- * @version CVS: $Id: Reader.php,v 1.13 2005/02/26 00:15:41 vincentlascaux Exp $
+ * @version CVS: $Id: Reader.php,v 1.14 2005/04/16 08:59:15 vincentlascaux Exp $
* @link http://pear.php.net/package/File_Archive
*/
@@ -124,10 +124,8 @@
* Returns an array of statistics about the file
* (see the PHP stat function for more information)
*
- * The only element that must be present in the array
- * is the size (index 7)
- * All the other element may not be present if the reader
- * doesnt know about it
+ * The returned array may be empty, even if readers should try
+ * their best to return as many data as possible
*/
function getStat() { return array(); }
@@ -224,9 +222,6 @@
function extract(&$writer, $autoClose = true, $bufferSize = 102400)
{
while (($error = $this->next()) === true) {
- $filename = $this->getFilename();
- $stat = $this->getStat();
-
$error = $writer->newFile(
$this->getFilename(),
$this->getStat(),
http://cvs.php.net/diff.php/pear/File_Archive/Archive/Writer/Tar.php?r1=1.9&r2=1.10&ty=u
Index: pear/File_Archive/Archive/Writer/Tar.php
diff -u pear/File_Archive/Archive/Writer/Tar.php:1.9 pear/File_Archive/Archive/Writer/Tar.php:1.10
--- pear/File_Archive/Archive/Writer/Tar.php:1.9 Fri Feb 25 16:31:37 2005
+++ pear/File_Archive/Archive/Writer/Tar.php Sat Apr 16 04:59:18 2005
@@ -25,17 +25,24 @@
* @author Vincent Lascaux <vincentlascaux@php.net>
* @copyright 1997-2005 The PHP Group
* @license http://www.gnu.org/copyleft/lesser.html LGPL
- * @version CVS: $Id: Tar.php,v 1.9 2005/02/25 21:31:37 vincentlascaux Exp $
+ * @version CVS: $Id: Tar.php,v 1.10 2005/04/16 08:59:18 vincentlascaux Exp $
* @link http://pear.php.net/package/File_Archive
*/
-require_once "MemoryArchive.php";
+require_once "Archive.php";
/**
* Write the files as a TAR archive
*/
-class File_Archive_Writer_Tar extends File_Archive_Writer_MemoryArchive
+class File_Archive_Writer_Tar extends File_Archive_Writer_Archive
{
+ var $buffer;
+ var $useBuffer;
+
+ var $filename = null;
+ var $stats = null;
+
+
/**
* Creates the TAR header for a file
*
@@ -46,6 +53,7 @@
*/
function tarHeader($filename, $stat)
{
+ echo "header ($filename)\n";
$mode = isset($stat[2]) ? $stat[2] : 0x8000;
$uid = isset($stat[4]) ? $stat[4] : 0;
$gid = isset($stat[5]) ? $stat[5] : 0;
@@ -111,14 +119,14 @@
/**
* Creates the TAR footer for a file
*
- * @param array $stat the statistics of the file
+ * @param int $size the size of the data that has been written to the TAR
* @return string A string made of less than 512 characteres to fill the
* last 512 byte long block
* @access private
*/
- function tarFooter($stat)
+ function tarFooter($size)
{
- $size = $stat[7];
+ echo "footer\n";
if ($size % 512 > 0) {
return pack("a".(512 - $size%512), "");
} else {
@@ -126,64 +134,73 @@
}
}
- /**
- * @see File_Archive_Writer_MemoryArchive::appendFile()
- * @access protected
- */
- function appendFile($filename, $dataFilename)
+ function flush()
{
- $stat = stat($dataFilename);
-
- $error = $this->innerWriter->writeData(
- $this->tarHeader($filename, $stat)
- );
- if (PEAR::isError($error)) {
- return $error;
- }
- $error = $this->innerWriter->writeFile($dataFilename);
- if (PEAR::isError($error)) {
- return $error;
+ if ($this->filename != null) {
+ if ($this->useBuffer) {
+ $this->stats[7] = strlen($this->buffer);
+
+ $this->innerWriter->writeData(
+ $this->tarHeader($this->filename, $this->stats)
+ );
+ $this->innerWriter->writeData(
+ $this->buffer
+ );
+ }
+ $this->innerWriter->writeData(
+ $this->tarFooter($this->stats[7])
+ );
}
- $error = $this->innerWriter->writeData(
- $this->tarFooter($stat)
- );
- if (PEAR::isError($error)) {
- return $error;
+ $this->buffer = "";
+ }
+
+ function newFile($filename, $stats = array(),
+ $mime = "application/octet-stream")
+ {
+ $this->flush();
+
+ $this->useBuffer = !isset($stats[7]);
+ $this->filename = $filename;
+ $this->stats = $stats;
+
+ if(!$this->useBuffer) {
+ return $this->innerWriter->writeData(
+ $this->tarHeader($filename, $stats)
+ );
}
}
+
/**
- * @see File_Archive_Writer_MemoryArchive::appendFileData()
- * @access protected
+ * @see File_Archive_Writer::close()
*/
- function appendFileData($filename, $stat, $data)
+ function close()
{
- $size = strlen($data);
- $stat[7] = $size;
-
- $error = $this->innerWriter->writeData(
- $this->tarHeader($filename, $stat)
- );
- if (PEAR::isError($error)) {
- return $error;
- }
- $error = $this->innerWriter->writeData($data);
- if (PEAR::isError($error)) {
- return $error;
- }
- $error = $this->innerWriter->writeData(
- $this->tarFooter($stat)
- );
- if (PEAR::isError($error)) {
- return $error;
+ $this->flush();
+ $this->innerWriter->writeData(pack("a1024", ""));
+ parent::close();
+ }
+ /**
+ * @see File_Archive_Writer::writeData()
+ */
+ function writeData($data)
+ {
+ if ($this->useBuffer) {
+ $this->buffer .= $data;
+ } else {
+ $this->innerWriter->writeData($data);
}
+
}
/**
- * @see File_Archive_Writer_MemoryArchive::sendFooter()
- * @access protected
+ * @see File_Archive_Writer::writeFile()
*/
- function sendFooter()
+ function writeFile($filename)
{
- return $this->innerWriter->writeData(pack("a1024", ""));
+ if ($this->useBuffer) {
+ $this->buffer .= file_get_contents($filename);
+ } else {
+ $this->innerWriter->writeFile($filename);
+ }
}
/**
* @see File_Archive_Writer::getMime()
http://cvs.php.net/diff.php/pear/File_Archive/tests/test.php?r1=1.22&r2=1.23&ty=u
Index: pear/File_Archive/tests/test.php
diff -u pear/File_Archive/tests/test.php:1.22 pear/File_Archive/tests/test.php:1.23
--- pear/File_Archive/tests/test.php:1.22 Fri Apr 15 19:36:46 2005
+++ pear/File_Archive/tests/test.php Sat Apr 16 04:59:18 2005
@@ -249,7 +249,7 @@
$source = File_Archive::readSource($compressed->makeReader(),
"$filename/$generated");
if(PEAR::isError($source))
- var_dump($source);
+ echo ($extension);
$source->extract(File_Archive::toVariable($uncompressed));
$this->assertEquals(file_get_contents('test.php'), $uncompressed);