UniqueAppender.php
4.55 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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* A writer wrapper that will remove the files the eventual duplicate
* files from the reader to keep only the new ones
* When calling newFile, the file with the highest index in the reader
* and the same filename will be removed
* Note that it ensure that the archive won't have duplicates only if
* it didn't have duplicates before, and if no two calls to newFile with
* the same filename is done
*
* 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: UniqueAppender.php,v 1.1 2005/05/30 19:44:53 vincentlascaux Exp $
* @link http://pear.php.net/package/File_Archive
*/
require_once "File/Archive/Writer.php";
require_once "File/Archive/Reader.php";
require_once "File/Archive/Predicate/Index.php";
/**
* A writer wrapper that will remove the files the eventual duplicate
* files from the reader to keep only the new ones
* If there were already some duplications in the provided reader, not
* all duplication will be removed
* If you use newFile with the same filename several file, only the latest
* write will be kept (no time comparision is done)
*/
class File_Archive_Writer_UniqueAppender extends File_Archive_Writer
{
var $reader;
var $writer;
var $fileList = array();
var $toDelete = array();
/**
* Construct a unique writer that will write to the specified writer
* and remove duplicate files from the reader on close
*/
function File_Archive_Writer_UniqueAppender(&$reader)
{
$reader->close();
$pos = 0;
while ($reader->next()) {
$this->fileList[$reader->getFilename()] = $pos++;
}
$this->reader =& $reader;
$this->writer = $reader->makeAppendWriter();
}
/**
* @see File_Archive_Writer::newFile()
*/
function newFile($filename, $stat = array(), $mime = "application/octet-stream")
{
if (isset($this->fileList[$filename])) {
$this->toDelete[$this->fileList[$filename]] = true;
}
return $this->writer->newFile($filename, $stat, $mime);
}
/**
* @see File_Archive_Writer::newFromTempFile()
*/
function newFromTempFile($tmpfile, $filename, $stat = array(), $mime = "application/octet-stream")
{
if (isset($this->fileList[$filename])) {
$this->toDelete[$this->fileList[$filename]] = true;
}
return $this->writer->newFromTempFile($tmpfile, $filename, $stat, $mime);
}
/**
* @see File_Archive_Writer::newFileNeedsMIME()
*/
function newFileNeedsMIME()
{
return $this->writer->newFileNeedsMIME();
}
/**
* @see File_Archive_Writer::writeData()
*/
function writeData($data)
{
return $this->writer->writeData($data);
}
/**
* @see File_Archive_Writer::writeFile()
*/
function writeFile($filename)
{
return $this->writer->writeFile($filename);
}
/**
* Close the writer, eventually flush the data, write the footer...
* This function must be called before the end of the script
*/
function close()
{
$error = $this->writer->close();
if (PEAR::isError($error)) {
return $error;
}
if (!empty($this->toDelete)) {
$tmp = $this->reader->makeWriterRemoveFiles(
new File_Archive_Predicate_Index($this->toDelete)
);
if (PEAR::isError($tmp)) {
return $tmp;
}
return $tmp->close();
}
}
}
?>