Skip to content

Commit 87031e0

Browse files
committed
Added Zip Adapters (PclZip & ZipArchive)
1 parent 9aa1f7a commit 87031e0

File tree

9 files changed

+276
-105
lines changed

9 files changed

+276
-105
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@
3333

3434
### Changes
3535
- XMLWriter : Refactoring for improving performances
36+
37+
## 0.2.5
38+
39+
### Features
40+
- Added Zip Adapter (PclZip & ZipArchive)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
}
1616
],
1717
"require": {
18-
"php": ">=5.3.0"
18+
"php": ">=5.3.0",
19+
"pclzip/pclzip": "^2.8"
1920
},
2021
"require-dev": {
2122
"phpunit/phpunit": "3.7.*",
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
namespace PhpOffice\Common\Adapter\Zip;
3+
4+
use PclZip;
5+
6+
class PclZipAdapter implements ZipInterface
7+
{
8+
/**
9+
* @var PclZip
10+
*/
11+
protected $oPclZip;
12+
13+
/**
14+
* @var string
15+
*/
16+
protected $tmpDir;
17+
18+
/**
19+
* @param $filename
20+
* @return $this
21+
*/
22+
public function open($filename)
23+
{
24+
$this->oPclZip = new PclZip($filename);
25+
$this->tmpDir = sys_get_temp_dir();
26+
return $this;
27+
}
28+
29+
/**
30+
* @return $this
31+
*/
32+
public function close()
33+
{
34+
return $this;
35+
}
36+
37+
/**
38+
* @param $localname
39+
* @param $contents
40+
* @return $this
41+
* @throws \Exception
42+
*/
43+
public function addFromString($localname, $contents)
44+
{
45+
$pathData = pathinfo($localname);
46+
47+
$hFile = fopen($this->tmpDir.'/'.$pathData['basename'], "wb");
48+
fwrite($hFile, $contents);
49+
fclose($hFile);
50+
51+
$res = $this->oPclZip->add($this->tmpDir.'/'.$pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']);
52+
if ($res == 0) {
53+
throw new \Exception("Error zipping files : " . $this->oPclZip->errorInfo(true));
54+
}
55+
unlink($this->tmpDir.'/'.$pathData['basename']);
56+
return $this;
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace PhpOffice\Common\Adapter\Zip;
4+
5+
use ZipArchive;
6+
7+
class ZipArchiveAdapter implements ZipInterface
8+
{
9+
/**
10+
* @var ZipArchive
11+
*/
12+
protected $oZipArchive;
13+
14+
/**
15+
* @var string
16+
*/
17+
protected $filename;
18+
19+
/**
20+
* @param string $filename
21+
* @throws \Exception Could not open $this->filename for writing.
22+
* @return mixed
23+
*/
24+
public function open($filename){
25+
$this->filename = $filename;
26+
$this->oZipArchive = new ZipArchive();
27+
28+
if ($this->oZipArchive->open($this->filename, ZipArchive::OVERWRITE) === true) {
29+
return $this;
30+
}
31+
if ($this->oZipArchive->open($this->filename, ZipArchive::CREATE) === true) {
32+
return $this;
33+
}
34+
throw new \Exception("Could not open $this->filename for writing.");
35+
}
36+
37+
/**
38+
* @return $this
39+
* @throws \Exception Could not close zip file $this->filename.
40+
*/
41+
public function close()
42+
{
43+
if ($this->oZipArchive->close() === false) {
44+
throw new \Exception("Could not close zip file $this->filename.");
45+
}
46+
return $this;
47+
}
48+
49+
/**
50+
* @param $localname
51+
* @param $contents
52+
* @return bool
53+
*/
54+
public function addFromString($localname, $contents)
55+
{
56+
return $this->oZipArchive->addFromString($localname, $contents);
57+
}
58+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace PhpOffice\Common\Adapter\Zip;
4+
5+
6+
interface ZipInterface
7+
{
8+
public function open($filename);
9+
public function close();
10+
public function addFromString($localname, $contents);
11+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Common\Tests\Adapter\Zip;
4+
5+
use PhpOffice\Common\Adapter\Zip\PclZipAdapter;
6+
use PhpOffice\Common\Tests\TestHelperZip;
7+
8+
class PclZipAdapterTest extends \PHPUnit_Framework_TestCase
9+
{
10+
protected $zipTest;
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
17+
$this->zipTest = tempnam($pathResources, 'PhpOfficeCommon');
18+
copy($pathResources.'Sample_01_Simple.pptx', $this->zipTest);
19+
}
20+
21+
public function tearDown()
22+
{
23+
parent::tearDown();
24+
25+
unlink($this->zipTest);
26+
}
27+
28+
public function testOpen()
29+
{
30+
$object = new PclZipAdapter();
31+
$this->assertInstanceOf('PhpOffice\\Common\\Adapter\\Zip\\ZipInterface', $object->open($this->zipTest));
32+
}
33+
34+
public function testClose()
35+
{
36+
$object = new PclZipAdapter();
37+
$object->open($this->zipTest);
38+
$this->assertInstanceOf('PhpOffice\\Common\\Adapter\\Zip\\ZipInterface', $object->close());
39+
}
40+
41+
public function testAddFromString()
42+
{
43+
$expectedPath = 'file.test';
44+
$expectedContent = 'Content';
45+
46+
$object = new PclZipAdapter();
47+
$object->open($this->zipTest);
48+
$object->addFromString($expectedPath, $expectedContent);
49+
$object->close();
50+
51+
$this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath));
52+
$this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent));
53+
}
54+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Common\Tests\Adapter\Zip;
4+
5+
use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter;
6+
use PhpOffice\Common\Tests\TestHelperZip;
7+
8+
9+
class ZipArchiveAdapterTest extends \PHPUnit_Framework_TestCase
10+
{
11+
protected $zipTest;
12+
13+
public function setUp()
14+
{
15+
parent::setUp();
16+
17+
$pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
18+
$this->zipTest = tempnam($pathResources, 'PhpOfficeCommon');
19+
copy($pathResources.'Sample_01_Simple.pptx', $this->zipTest);
20+
}
21+
22+
public function tearDown()
23+
{
24+
parent::tearDown();
25+
26+
unlink($this->zipTest);
27+
}
28+
29+
public function testOpen()
30+
{
31+
$object = new ZipArchiveAdapter();
32+
$this->assertInstanceOf('PhpOffice\\Common\\Adapter\\Zip\\ZipInterface', $object->open($this->zipTest));
33+
}
34+
35+
public function testClose()
36+
{
37+
$object = new ZipArchiveAdapter();
38+
$object->open($this->zipTest);
39+
$this->assertInstanceOf('PhpOffice\\Common\\Adapter\\Zip\\ZipInterface', $object->close());
40+
}
41+
42+
public function testAddFromString()
43+
{
44+
$expectedPath = 'file.test';
45+
$expectedContent = 'Content';
46+
47+
$object = new ZipArchiveAdapter();
48+
$object->open($this->zipTest);
49+
$object->addFromString($expectedPath, $expectedContent);
50+
$object->close();
51+
52+
$this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath));
53+
$this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent));
54+
}
55+
}

tests/Common/Tests/_includes/TestHelperDOCX.php

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)