1<?php 2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 4/** 5 * Compress a single file to Bzip2 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$ 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 Bzip2 format 36 */ 37class File_Archive_Writer_Bzip2 extends File_Archive_Writer 38{ 39 40 /** 41 * compressionLevel 42 * 43 * @var integer 44 * @access public 45 * @deprecated 46 */ 47 var $compressionLevel=9; 48 var $bzfile; 49 var $tmpName; 50 var $nbFiles = 0; 51 52 var $innerWriter; 53 var $autoClose; 54 var $filename; 55 var $stat; 56 57 /** 58 * @param string $filename Name to give to the archive 59 * @param File_Archive_Writer $innerWriter The inner writer to which the 60 * compressed data will be written 61 * @param array $stat The stat of the archive (see the PHP stat() function). 62 * No element are required in this array 63 * @param bool $autoClose Indicate if the inner writer must be closed when 64 * closing this 65 */ 66 function File_Archive_Writer_Bzip2($filename, &$innerWriter, 67 $stat = array(), $autoClose = true) 68 { 69 $this->innerWriter =& $innerWriter; 70 $this->autoClose = $autoClose; 71 72 $this->filename = $filename; 73 $this->stat = $stat; 74 75 if ($this->filename === null) { 76 $this->newFile(null); 77 } 78 } 79 80 /** 81 * Set the compression level. Do nothing because PHP bz2 ext doesn't 82 * support this. 83 * 84 * @param int $compressionLevel From 0 (no compression) to 9 (best 85 * compression) 86 * @deprecated 87 */ 88 function setCompressionLevel($compressionLevel) 89 { 90 $this->compressionLevel = $compressionLevel; 91 } 92 93 /** 94 * @see File_Archive_Writer::newFile() 95 * 96 * Check that one single file is written in the BZip2 archive 97 */ 98 function newFile($filename, $stat = array(), 99 $mime = "application/octet-stream") 100 { 101 if ($this->nbFiles > 1) { 102 return PEAR::raiseError("A Bzip2 archive can only contain one single file.". 103 "Use Tbz archive to be able to write several files"); 104 } 105 $this->nbFiles++; 106 107 $this->tmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far'); 108 $this->bzfile = bzopen($this->tmpName, 'w'); 109 110 return true; 111 } 112 113 /** 114 * Actually write the tmp file to the inner writer 115 * Close and delete temporary file 116 * 117 * @see File_Archive_Writer::close() 118 */ 119 function close() 120 { 121 bzclose($this->bzfile); 122 123 if ($this->filename === null) { 124 //Assume innerWriter is already opened on a file... 125 $this->innerWriter->writeFile($this->tmpName); 126 unlink($this->tmpName); 127 } else { 128 $this->innerWriter->newFromTempFile( 129 $this->tmpName, $this->filename, $this->stat, 'application/x-compressed' 130 ); 131 } 132 133 if ($this->autoClose) { 134 return $this->innerWriter->close(); 135 } 136 } 137 138 /** 139 * @see File_Archive_Writer::writeData() 140 */ 141 function writeData($data) 142 { 143 bzwrite($this->bzfile, $data); 144 } 145} 146 147?> 148