cvs: pear /File_Archive/Archive/Reader Gzip.php /File_Archive/Archive/Writer Archive.php Bzip2.php Gzip.php /File_Archive/tests test.php

From: Date: Fri, 15 Apr 2005 21:52:03 +0000
Subject: cvs: pear /File_Archive/Archive/Reader Gzip.php /File_Archive/Archive/Writer Archive.php Bzip2.php Gzip.php /File_Archive/tests test.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-31185@lists.php.net to get a copy of this message
vincentlascaux Fri Apr 15 17:52:03 2005 EDT Modified files: /pear/File_Archive/Archive/Reader Gzip.php /pear/File_Archive/Archive/Writer Archive.php Bzip2.php Gzip.php /pear/File_Archive/tests test.php Log: + GZip file reader will always considere that the name of the compressed file is the one of the archive without the extension. This is more coherent with BZ2 reader, and the user does not have to know if a name has been specified or not to be able to use it + Gzip and BZ2 writers don't load the whole file in memory but use a temporary file to generate the archive (memory consumption is now independant from the size of the generated archive)

http://cvs.php.net/diff.php/pear/File_Archive/Archive/Reader/Gzip.php?r1=1.10&r2=1.11&ty=u Index: pear/File_Archive/Archive/Reader/Gzip.php diff -u pear/File_Archive/Archive/Reader/Gzip.php:1.10 pear/File_Archive/Archive/Reader/Gzip.php:1.11 --- pear/File_Archive/Archive/Reader/Gzip.php:1.10 Sat Feb 26 05:34:23 2005 +++ pear/File_Archive/Archive/Reader/Gzip.php Fri Apr 15 17:52:01 2005 @@ -25,7 +25,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: Gzip.php,v 1.10 2005/02/26 10:34:23 vincentlascaux Exp $ + * @version CVS: $Id: Gzip.php,v 1.11 2005/04/15 21:52:01 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ @@ -40,7 +40,6 @@ var $offset = 0; var $alreadyRead = false; - var $name = null; var $comment = null; var $hasName = false; var $hasComment = false; @@ -81,31 +80,8 @@ } $temp = decbin($id['flags']); - $this->hasName = ($temp & 0x8); $this->hasComment = ($temp & 0x4); - $this->name = ""; - if ($this->hasName) { - while (($char = $this->source->getData(1)) !== "\0") { - if ($char === null) { - return PEAR::raiseError( - "Not valid gz file (unexpected end of archive ". - "reading filename)" - ); - } - if (PEAR::isError($char)) { - return $char; - } - $this->name .= $char; - } - $this->name = $this->getStandardURL($this->name); - } else { - $this->name = $this->source->getFilename(); - $pos = strrpos($name, "."); - if ($pos !== false && $pos !== 0) { - $this->name = substr($this->name, 0, $pos); - } - } $this->comment = ""; if ($this->hasComment) { while (($char = $this->source->getData(1)) !== "\0") { @@ -145,13 +121,24 @@ } return true; } + /** + * Return the name of the single file contained in the archive + * deduced from the name of the archive (the extension is removed) + * * @see File_Archive_Reader::getFilename() */ function getFilename() { - return $this->name; + $name = $this->source->getFilename(); + $pos = strrpos($name, "."); + if ($pos === false || $pos === 0) { + return $name; + } else { + return substr($name, 0, $pos); + } } + /** * @see File_Archive_Reader::getStat() */ http://cvs.php.net/diff.php/pear/File_Archive/Archive/Writer/Archive.php?r1=1.8&r2=1.9&ty=u Index: pear/File_Archive/Archive/Writer/Archive.php diff -u pear/File_Archive/Archive/Writer/Archive.php:1.8 pear/File_Archive/Archive/Writer/Archive.php:1.9 --- pear/File_Archive/Archive/Writer/Archive.php:1.8 Thu Feb 24 05:48:56 2005 +++ pear/File_Archive/Archive/Writer/Archive.php Fri Apr 15 17:52:02 2005 @@ -26,7 +26,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: Archive.php,v 1.8 2005/02/24 10:48:56 vincentlascaux Exp $ + * @version CVS: $Id: Archive.php,v 1.9 2005/04/15 21:52:02 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ @@ -41,7 +41,7 @@ /** * @var File_Archive_Writer The compressed data will be written to this * writer - * @access private + * @access protected */ var $innerWriter; http://cvs.php.net/diff.php/pear/File_Archive/Archive/Writer/Bzip2.php?r1=1.2&r2=1.3&ty=u Index: pear/File_Archive/Archive/Writer/Bzip2.php diff -u pear/File_Archive/Archive/Writer/Bzip2.php:1.2 pear/File_Archive/Archive/Writer/Bzip2.php:1.3 --- pear/File_Archive/Archive/Writer/Bzip2.php:1.2 Sat Feb 26 05:34:24 2005 +++ pear/File_Archive/Archive/Writer/Bzip2.php Fri Apr 15 17:52:02 2005 @@ -25,18 +25,22 @@ * @author Vincent Lascaux <vincentlascaux@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.gnu.org/copyleft/lesser.html LGPL - * @version CVS: $Id: Bzip2.php,v 1.2 2005/02/26 10:34:24 vincentlascaux Exp $ + * @version CVS: $Id: Bzip2.php,v 1.3 2005/04/15 21:52:02 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ -require_once "MemoryArchive.php"; +require_once "Archive.php"; /** * Compress a single file to Bzip2 format */ -class File_Archive_Writer_Bzip2 extends File_Archive_Writer_MemoryArchive +class File_Archive_Writer_Bzip2 extends File_Archive_Writer_Archive { var $compressionLevel=9; + var $bzfile; + var $tmpName; + var $nbFiles = 0; + /** * Set the compression level * @@ -56,26 +60,37 @@ function newFile($filename, $stat = array(), $mime = "application/octet-stream") { - $result = parent::newFile($filename, $stat, $mime); - if ($result !== true) { - return $result; - } if($this->nbFiles > 1) { return PEAR::raiseError("A Bzip2 archive can only contain one single file.". "Use Tbz archive to be able to write several files"); } + $this->nbFiles++; + + $this->tmpName = tempnam('.', 'far'); + $this->bzfile = bzopen($this->tmpName, 'w'.$this->compressionLevel); + return true; } /** - * @see File_Archive_Writer_MemoryArchive::appendFileData() + * Actually write the tmp file to the inner writer + * Close and delete temporary file + * + * @see File_Archive_Writer::close(); */ - function appendFileData($filename, $stat, $data) + function close() + { + gzclose($this->bzfile); + $this->innerWriter->writeFile($this->tmpName); + unlink($this->tmpName); + + return parent::close(); + } + + function writeData($data) { - return $this->innerWriter->writeData( - bzcompress($data, $this->compressionLevel) - ); + bzwrite($this->bzfile, $data); } } http://cvs.php.net/diff.php/pear/File_Archive/Archive/Writer/Gzip.php?r1=1.8&r2=1.9&ty=u Index: pear/File_Archive/Archive/Writer/Gzip.php diff -u pear/File_Archive/Archive/Writer/Gzip.php:1.8 pear/File_Archive/Archive/Writer/Gzip.php:1.9 --- pear/File_Archive/Archive/Writer/Gzip.php:1.8 Sat Feb 26 05:34:24 2005 +++ pear/File_Archive/Archive/Writer/Gzip.php Fri Apr 15 17:52:02 2005 @@ -25,28 +25,22 @@ * @author Vincent Lascaux <vincentlascaux@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.gnu.org/copyleft/lesser.html LGPL - * @version CVS: $Id: Gzip.php,v 1.8 2005/02/26 10:34:24 vincentlascaux Exp $ + * @version CVS: $Id: Gzip.php,v 1.9 2005/04/15 21:52:02 vincentlascaux Exp $ * @link http://pear.php.net/package/File_Archive */ -require_once "MemoryArchive.php"; +require_once "Archive.php"; /** * Compress a single file to Gzip format */ -class File_Archive_Writer_Gzip extends File_Archive_Writer_MemoryArchive +class File_Archive_Writer_Gzip extends File_Archive_Writer_Archive { var $comment = ""; var $compressionLevel = 9; - - /** - * The Gzip file format allows a comment to be put at the end of the file - * By default, no comments are put there - * You can change this by calling setComment - * - * @param string $comment - */ - function setComment($comment) { $this->comment = $comment; } + var $gzfile; + var $tmpName; + var $nbFiles = 0; /** * Set the compression level @@ -67,32 +61,35 @@ function newFile($filename, $stat = array(), $mime = "application/octet-stream") { - $result = parent::newFile($filename, $stat, $mime); - if ($result !== true) { - return $result; - } if($this->nbFiles > 1) { return PEAR::raiseError("A GZip archive can only contain one single file.". "Use Tbz archive to be able to write several files"); } + $this->nbFiles++; + + $this->tmpName = tempnam('.', 'far'); + $this->gzfile = gzopen($this->tmpName, 'w'.$this->compressionLevel); + return true; } - /** - * @see File_Archive_Writer_MemoryArchive::appendFileData() + * Actually write the tmp file to the inner writer + * Close and delete temporary file + * + * @see File_Archive_Writer::close(); */ - function appendFileData($filename, $stat, $data) + function close() { - $flags = (empty($this->comment)? 0 : 16) + (empty($filename)? 0 : 8); - $mtime = isset($stat[9]) ? $stat[9] : time(); + gzclose($this->gzfile); + $this->innerWriter->writeFile($this->tmpName); + unlink($this->tmpName); - return $this->innerWriter->writeData( - pack("C1C1C1C1VC1C1",0x1f,0x8b,8,$flags,$mtime,2,0xFF). - (empty($filename) ? "" : $filename."\0"). - (empty($this->comment) ? "" : $this->comment."\0"). - gzdeflate($data, $this->compressionLevel). - pack("VV",crc32($data),strlen($data)) - ); + return parent::close(); + } + + function writeData($data) + { + gzwrite($this->gzfile, $data); } } http://cvs.php.net/diff.php/pear/File_Archive/tests/test.php?r1=1.20&r2=1.21&ty=u Index: pear/File_Archive/tests/test.php diff -u pear/File_Archive/tests/test.php:1.20 pear/File_Archive/tests/test.php:1.21 --- pear/File_Archive/tests/test.php:1.20 Fri Apr 8 06:18:06 2005 +++ pear/File_Archive/tests/test.php Fri Apr 15 17:52:02 2005 @@ -236,6 +236,7 @@ $filename = "test.$extension"; $source = File_Archive::read('test.php'); + $generated = $extension=='gz' || $extension=='bz2' ? 'test' : 'test.php'; $source->extract( File_Archive::toArchive( @@ -246,7 +247,9 @@ require_once "File/Archive/Reader/Uncompress.php"; - $source = File_Archive::readSource($compressed->makeReader(), "$filename/test.php"); + $source = File_Archive::readSource($compressed->makeReader(), "$filename/$generated"); + if(PEAR::isError($source)) + var_dump($source); $source->extract(File_Archive::toVariable($uncompressed)); $this->assertEquals(file_get_contents('test.php'), $uncompressed); @@ -255,7 +258,8 @@ function testZip() { $this->_testArchive('zip'); } function testGzip() { $this->_testArchive('gz'); } function testTgz() { $this->_testArchive('tgz'); } - function testTbz() { $this->_testArchive('tbz');} + function testTbz() { $this->_testArchive('tbz'); } + function testBZ2() { $this->_testArchive('bz2'); } function testWriteBZip2() { //Build the writer
« previous php.pear.cvs (#31185) next »