forked from Vaporbook/BookGluttonEpub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OreillyZipListing.php
executable file
·113 lines (92 loc) · 1.95 KB
/
OreillyZipListing.php
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
<?php
/*
Originally used for zip bundles of Oreilly titles
*/
class OreillyZipListing {
public function __construct($z)
{
// construct with full path to zip file
$this->z = $z;
$this->files = array();
$this->handles = array();
$this->log("reading zip file ".$z);
$this->_open();
if(!is_resource($this->zh)) {
$this->log('error '.$this->zh);
exit;
} else {
while($e = zip_read($this->zh)) {
$this->files[] = array(
'name'=>zip_entry_name($e),
'size'=>zip_entry_filesize($e)
);
$this->handles[zip_entry_name($e)] = $e;
}
}
$this->_close();
}
public function getFiles()
{
return $this->files;
}
public function findFile($name, $linkonly=false)
{
$this->log('seeking '.$name);
$data = null;
foreach($this->files as $file) {
$basename = pathinfo($file['name'], PATHINFO_BASENAME);
$this->log($basename);
if($basename==$name) {
if($linkonly) {
$data = 'zip://' . $this->z . '#'.$file['name'];
} else {
$data = $this->getFile($file['name']);
}
break;
} else {
//echo "$basename==$name\n";
}
}
return $data;
}
public function getFile($name)
{
$this->_open();
$contents = '';
while($e = zip_read($this->zh)) {
if(zip_entry_name($e)==$name) {
if(zip_entry_open($this->zh, $e)) {
$size = zip_entry_filesize($e);
while($data = zip_entry_read($e,$size)) {
$contents .= $data;
}
}
}
}
/*
$e = $this->handles[$name];
if(zip_entry_open($this->zh, $e)) {
$size = zip_entry_filesize($e);
while($data = zip_entry_read($e,$size)) {
$contents .= $data;
}
} else {
$this->log('could not open');
}
*/
$this->_close();
return $contents;
}
public function log($msg, $level=0)
{
//echo $msg . "\n";
}
private function _close()
{
zip_close($this->zh);
}
private function _open()
{
$this->zh = zip_open($this->z);
}
}