-
Notifications
You must be signed in to change notification settings - Fork 58
/
PclZipAdapter.php
46 lines (36 loc) · 1.04 KB
/
PclZipAdapter.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
<?php
namespace PhpOffice\Common\Adapter\Zip;
class PclZipAdapter implements ZipInterface
{
/**
* @var \PclZip
*/
protected $oPclZip;
/**
* @var string
*/
protected $tmpDir;
public function open($filename)
{
$this->oPclZip = new \PclZip($filename);
$this->tmpDir = sys_get_temp_dir();
return $this;
}
public function close()
{
return $this;
}
public function addFromString($localname, $contents)
{
$pathData = pathinfo($localname);
$hFile = fopen($this->tmpDir . '/' . $pathData['basename'], 'wb');
fwrite($hFile, $contents);
fclose($hFile);
$res = $this->oPclZip->add($this->tmpDir . '/' . $pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']);
if ($res == 0) {
throw new \Exception('Error zipping files : ' . $this->oPclZip->errorInfo(true));
}
unlink($this->tmpDir . '/' . $pathData['basename']);
return $this;
}
}