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: Bzip2.php,v 1.9 2005/06/02 16:22:47 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 Bzip2 format 36 */ 37class File_Archive_Writer_Bzip2 extends File_Archive_Writer 38{ 39 var $compressionLevel=9; 40 var $bzfile; 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_Bzip2($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 72 /** 73 * Set the compression level 74 * 75 * @param int $compressionLevel From 0 (no compression) to 9 (best 76 * compression) 77 */ 78 function setCompressionLevel($compressionLevel) 79 { 80 $this->compressionLevel = $compressionLevel; 81 } 82 83 /** 84 * @see File_Archive_Writer::newFile() 85 * 86 * Check that one single file is written in the BZip2 archive 87 */ 88 function newFile($filename, $stat = array(), 89 $mime = "application/octet-stream") 90 { 91 if ($this->nbFiles > 1) { 92 return PEAR::raiseError("A Bzip2 archive can only contain one single file.". 93 "Use Tbz archive to be able to write several files"); 94 } 95 $this->nbFiles++; 96 97 $this->tmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far'); 98 $this->bzfile = bzopen($this->tmpName, 'w'.$this->compressionLevel); 99 100 return true; 101 } 102 103 /** 104 * Actually write the tmp file to the inner writer 105 * Close and delete temporary file 106 * 107 * @see File_Archive_Writer::close() 108 */ 109 function close() 110 { 111 bzclose($this->bzfile); 112 113 if ($this->filename === null) { 114 //Assume innerWriter is already opened on a file... 115 $this->innerWriter->writeFile($this->tmpName); 116 unlink($this->tmpName); 117 } else { 118 $this->innerWriter->newFromTempFile( 119 $this->tmpName, $this->filename, $this->stat, 'application/x-compressed' 120 ); 121 } 122 123 if ($this->autoClose) { 124 return $this->innerWriter->close(); 125 } 126 } 127 128 /** 129 * @see File_Archive_Writer::writeData() 130 */ 131 function writeData($data) 132 { 133 bzwrite($this->bzfile, $data); 134 } 135} 136 137?>