1<?php 2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 4/** 5 * Compress a single file to Gzip format 6 * 7 * PHP versions 4 and 5 8 * 9 * This library is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU Lesser General Public 11 * License as published by the Free Software Foundation; either 12 * version 2.1 of the License, or (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA 22 * 23 * @category File Formats 24 * @package File_Archive 25 * @author Vincent Lascaux <vincentlascaux@php.net> 26 * @copyright 1997-2005 The PHP Group 27 * @license http://www.gnu.org/copyleft/lesser.html LGPL 28 * @version CVS: $Id: Gzip.php,v 1.15 2005/06/02 16:22:48 vincentlascaux Exp $ 29 * @link http://pear.php.net/package/File_Archive 30 */ 31 32require_once "File/Archive/Writer.php"; 33 34/** 35 * Compress a single file to Gzip format 36 */ 37class File_Archive_Writer_Gzip extends File_Archive_Writer 38{ 39 var $compressionLevel=9; 40 var $gzfile; 41 var $tmpName; 42 var $nbFiles = 0; 43 44 var $innerWriter; 45 var $autoClose; 46 var $filename; 47 var $stat; 48 49 /** 50 * @param string $filename Name to give to the archive 51 * @param File_Archive_Writer $innerWriter The inner writer to which the 52 * compressed data will be written 53 * @param array $stat The stat of the archive (see the PHP stat() function). 54 * No element are required in this array 55 * @param bool $autoClose Indicate if the inner writer must be closed when 56 * closing this 57 */ 58 function File_Archive_Writer_Gzip($filename, &$innerWriter, 59 $stat = array(), $autoClose = true) 60 { 61 $this->innerWriter =& $innerWriter; 62 $this->autoClose = $autoClose; 63 64 $this->filename = $filename; 65 $this->stat = $stat; 66 67 if ($this->filename === null) { 68 $this->newFile(null); 69 } 70 71 $compressionLevel = File_Archive::getOption('gzCompressionLevel', 9); 72 } 73 74 /** 75 * Set the compression level 76 * 77 * @param int $compressionLevel From 0 (no compression) to 9 (best 78 * compression) 79 */ 80 function setCompressionLevel($compressionLevel) 81 { 82 $this->compressionLevel = $compressionLevel; 83 } 84 85 /** 86 * @see File_Archive_Writer::newFile() 87 * 88 * Check that one single file is written in the GZip archive 89 */ 90 function newFile($filename, $stat = array(), 91 $mime = "application/octet-stream") 92 { 93 if ($this->nbFiles > 1) { 94 return PEAR::raiseError("A Gz archive can only contain one single file.". 95 "Use Tgz archive to be able to write several files"); 96 } 97 $this->nbFiles++; 98 99 $this->tmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far'); 100 $this->gzfile = gzopen($this->tmpName, 'w'.$this->compressionLevel); 101 102 return true; 103 } 104 105 106 /** 107 * Actually write the tmp file to the inner writer 108 * Close and delete temporary file 109 * 110 * @see File_Archive_Writer::close() 111 */ 112 function close() 113 { 114 gzclose($this->gzfile); 115 if ($this->filename === null) { 116 //Assume innerWriter is already opened on a file... 117 $this->innerWriter->writeFile($this->tmpName); 118 unlink($this->tmpName); 119 } else { 120 $this->innerWriter->newFromTempFile( 121 $this->tmpName, $this->filename, $this->stat, 'application/x-compressed' 122 ); 123 } 124 125 if ($this->autoClose) { 126 return $this->innerWriter->close(); 127 } 128 } 129 130 /** 131 * @see File_Archive_Writer::writeData() 132 */ 133 function writeData($data) 134 { 135 gzwrite($this->gzfile, $data); 136 } 137} 138 139?>