|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpZip\Model\Data; |
|
4
|
|
|
|
|
5
|
|
|
use PhpZip\Exception\ZipException; |
|
6
|
|
|
use PhpZip\Model\ZipData; |
|
7
|
|
|
use PhpZip\Model\ZipEntry; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ZipFileData. |
|
11
|
|
|
*/ |
|
12
|
|
|
class ZipFileData implements ZipData |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \SplFileInfo */ |
|
15
|
|
|
private $file; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* ZipStringData constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param ZipEntry $zipEntry |
|
21
|
|
|
* @param \SplFileInfo $fileInfo |
|
22
|
|
|
* |
|
23
|
|
|
* @throws ZipException |
|
24
|
|
|
*/ |
|
25
|
35 |
|
public function __construct(ZipEntry $zipEntry, \SplFileInfo $fileInfo) |
|
26
|
|
|
{ |
|
27
|
35 |
|
if (!$fileInfo->isFile()) { |
|
28
|
|
|
throw new ZipException('$fileInfo is not a file.'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
35 |
|
if (!$fileInfo->isReadable()) { |
|
32
|
|
|
throw new ZipException('$fileInfo is not readable.'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
35 |
|
$this->file = $fileInfo; |
|
36
|
35 |
|
$zipEntry->setUncompressedSize($fileInfo->getSize()); |
|
37
|
35 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @throws ZipException |
|
41
|
|
|
* |
|
42
|
|
|
* @return resource returns stream data |
|
43
|
|
|
*/ |
|
44
|
35 |
|
public function getDataAsStream() |
|
45
|
|
|
{ |
|
46
|
35 |
|
if (!$this->file->isReadable()) { |
|
47
|
|
|
throw new ZipException(sprintf('The %s file is no longer readable.', $this->file->getPathname())); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
35 |
|
return fopen($this->file->getPathname(), 'rb'); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws ZipException |
|
55
|
|
|
* |
|
56
|
|
|
* @return string returns data as string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getDataAsString() |
|
59
|
|
|
{ |
|
60
|
|
|
if (!$this->file->isReadable()) { |
|
61
|
|
|
throw new ZipException(sprintf('The %s file is no longer readable.', $this->file->getPathname())); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return file_get_contents($this->file->getPathname()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param resource $outStream |
|
69
|
|
|
* |
|
70
|
|
|
* @throws ZipException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function copyDataToStream($outStream) |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
|
|
$stream = $this->getDataAsStream(); |
|
76
|
|
|
stream_copy_to_stream($stream, $outStream); |
|
77
|
|
|
} finally { |
|
78
|
|
|
fclose($stream); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.