Bzip2.php
7.11 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Uncompress a file that was compressed in the 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.19 2005/07/26 09:06:03 vincentlascaux Exp $
* @link http://pear.php.net/package/File_Archive
*/
require_once "File/Archive/Reader/Archive.php";
require_once "File/Archive/Writer/Files.php";
/**
* Uncompress a file that was compressed in the Bzip2 format
*/
class File_Archive_Reader_Bzip2 extends File_Archive_Reader_Archive
{
var $nbRead = 0;
var $bzfile = null;
var $tmpName = null;
var $filePos = 0;
/**
* @see File_Archive_Reader::close()
*/
function close($innerClose = true)
{
if ($this->bzfile !== null)
bzclose($this->bzfile);
if ($this->tmpName !== null)
unlink($this->tmpName);
$this->bzfile = null;
$this->tmpName = null;
$this->nbRead = 0;
$this->filePos = 0;
return parent::close($innerClose);
}
/**
* @see File_Archive_Reader::next()
*/
function next()
{
if (!parent::next()) {
return false;
}
$this->nbRead++;
if ($this->nbRead > 1) {
return false;
}
$dataFilename = $this->source->getDataFilename();
if ($dataFilename !== null)
{
$this->tmpName = null;
$this->bzfile = @bzopen($dataFilename, 'r');
if ($this->bzfile === false) {
return PEAR::raiseError("bzopen failed to open $dataFilename");
}
} else {
$this->tmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far');
//Generate the tmp data
$dest = new File_Archive_Writer_Files();
$dest->newFile($this->tmpName);
$this->source->sendData($dest);
$dest->close();
$this->bzfile = bzopen($this->tmpName, 'r');
}
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()
{
$name = $this->source->getFilename();
$pos = strrpos($name, ".");
if ($pos === false || $pos === 0) {
return $name;
} else {
return substr($name, 0, $pos);
}
}
/**
* @see File_Archive_Reader::getData()
*/
function getData($length = -1)
{
if ($length == -1) {
$data = '';
do {
$newData = bzread($this->bzfile);
$data .= $newData;
} while ($newData != '');
$this->filePos += strlen($data);
} else if ($length == 0) {
return '';
} else {
$data = '';
//The loop is here to correct what appears to be a bzread bug
while (strlen($data) < $length) {
$newData = bzread($this->bzfile, $length - strlen($data));
if ($newData == '') {
break;
}
$data .= $newData;
}
$this->filePos += strlen($data);
}
return $data == '' ? null : $data;
}
/**
* @see File_Archive_Reader::rewind
*/
function rewind($length = -1)
{
$before = $this->filePos;
bzclose($this->bzfile);
if ($this->tmpName === null) {
$this->bzfile = bzopen($this->source->getDataFilename(), 'r');
} else {
$this->bzfile = bzopen($this->tmpName, 'r');
}
$this->filePos = 0;
if ($length != -1) {
$this->skip($before - $length);
}
return $before - $this->filePos;
}
/**
* @see File_Archive_Reader::tell()
*/
function tell()
{
return $this->filePos;
}
/**
* @see File_Archive_Reader::makeAppendWriter()
*/
function makeAppendWriter()
{
return PEAR::raiseError('Unable to append files to a bzip2 archive');
}
/**
* @see File_Archive_Reader::makeWriterRemoveFiles()
*/
function makeWriterRemoveFiles($pred)
{
return PEAR::raiseError('Unable to remove files from a bzip2 archive');
}
/**
* @see File_Archive_Reader::makeWriterRemoveBlocks()
*/
function makeWriterRemoveBlocks($blocks, $seek = 0)
{
require_once "File/Archive/Writer/Bzip2.php";
if ($this->nbRead == 0) {
return PEAR::raiseError('No file selected');
}
//Uncompress data to a temporary file
$tmp = tmpfile();
$expectedPos = $this->filePos + $seek;
$this->rewind();
//Read the begining of the file
while ($this->filePos < $expectedPos &&
($data = $this->getData(min($expectedPos - $this->filePos, 8192))) !== null) {
fwrite($tmp, $data);
}
$keep = false;
foreach ($blocks as $length) {
if ($keep) {
$expectedPos = $this->filePos + $length;
while ($this->filePos < $expectedPos &&
($data = $this->getData(min($expectedPos - $this->filePos, 8192))) !== null) {
fwrite($tmp, $data);
}
} else {
$this->skip($length);
}
$keep = !$keep;
}
if ($keep) {
//Read the end of the file
while(($data = $this->getData(8192)) !== null) {
fwrite($tmp, $data);
}
}
fseek($tmp, 0);
//Create the writer
$this->source->rewind();
$innerWriter = $this->source->makeWriterRemoveBlocks(array()); //Truncate the source
unset($this->source);
$writer = new File_Archive_Writer_Bzip2(null, $innerWriter);
//And compress data from the temporary file
while (!feof($tmp)) {
$data = fread($tmp, 8192);
$writer->writeData($data);
}
fclose($tmp);
//Do not close inner writer since makeWriter was called
$this->close();
return $writer;
}
}
?>