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