Skip to content

Commit 05bb7bd

Browse files
authored
ZipAdapter: Allow to add a file with no compression (#55)
1 parent 1e968b2 commit 05bb7bd

File tree

12 files changed

+84
-28
lines changed

12 files changed

+84
-28
lines changed

.github/workflows/php.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
with:
7676
php-version: ${{ matrix.php }}
7777
extensions: gd, xml, zip
78-
coverage: xdebug
78+
coverage: ${{ (matrix.php == '7.3') && 'xdebug' || 'none' }}
7979

8080
- name: Generate Locale (for tests)
8181
run: sudo locale-gen de_DE.UTF-8 && sudo update-locale
@@ -86,6 +86,11 @@ jobs:
8686
run: composer install --ansi --prefer-dist --no-interaction --no-progress
8787

8888
- name: Run phpunit
89+
if: matrix.php != '7.3'
90+
run: ./vendor/bin/phpunit -c phpunit.xml.dist --no-coverage
91+
92+
- name: Run phpunit
93+
if: matrix.php == '7.3'
8994
run: ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover build/clover.xml
9095

9196
- name: Upload coverage results to Coveralls

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
[![Latest Stable Version](https://poser.pugx.org/phpoffice/common/v/stable.png)](https://packagist.org/packages/phpoffice/common)
2-
[![PHPOffice\Common](https://github.com/PHPOffice/Common/actions/workflows/php.yml/badge.svg)](https://github.com/PHPOffice/Common/actions/workflows/php.yml)
3-
[![Coverage Status](https://coveralls.io/repos/github/PHPOffice/Common/badge.svg?branch=develop)](https://coveralls.io/github/PHPOffice/Common?branch=develop)
4-
[![Total Downloads](https://poser.pugx.org/phpoffice/common/downloads.png)](https://packagist.org/packages/phpoffice/common)
5-
[![License](https://poser.pugx.org/phpoffice/common/license.png)](https://packagist.org/packages/phpoffice/common)
6-
[![Join the chat at https://gitter.im/PHPOffice/Common](https://img.shields.io/badge/GITTER-join%20chat-green.svg)](https://gitter.im/PHPOffice/Common)
1+
[![Latest Stable Version](https://poser.pugx.org/phpoffice/common/v)](https://packagist.org/packages/phpoffice/common)
2+
[![Coverage Status](https://coveralls.io/repos/github/PHPOffice/Common/badge.svg?branch=master)](https://coveralls.io/github/PHPOffice/Common?branch=master)
3+
[![Total Downloads](https://poser.pugx.org/phpoffice/common/downloads)](https://packagist.org/packages/phpoffice/common)
4+
[![License](https://poser.pugx.org/phpoffice/common/license)](https://packagist.org/packages/phpoffice/common)
75

6+
Branch Master : [![PHPOffice\Common](https://github.com/PHPOffice/Common/actions/workflows/php.yml/badge.svg?branch=master)](https://github.com/PHPOffice/Common/actions/workflows/php.yml)
87

98
PHPOffice Common is a library written in pure PHP that provides a set of components for PHPOffice librairies.
109

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.9
1+
1.0.3

src/Common/Adapter/Zip/PclZipAdapter.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,26 @@ public function close()
2727
return $this;
2828
}
2929

30-
public function addFromString($localname, $contents)
30+
public function addFromString(string $localname, string $contents, bool $withCompression = true)
3131
{
3232
$pathData = pathinfo($localname);
3333

3434
$hFile = fopen($this->tmpDir . '/' . $pathData['basename'], 'wb');
3535
fwrite($hFile, $contents);
3636
fclose($hFile);
3737

38-
$res = $this->oPclZip->add($this->tmpDir . '/' . $pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']);
38+
$params = [
39+
$this->tmpDir . '/' . $pathData['basename'],
40+
PCLZIP_OPT_REMOVE_PATH,
41+
$this->tmpDir,
42+
PCLZIP_OPT_ADD_PATH,
43+
$pathData['dirname'],
44+
];
45+
if (!$withCompression) {
46+
$params[] = PCLZIP_OPT_NO_COMPRESSION;
47+
}
48+
49+
$res = $this->oPclZip->add(...$params);
3950
if ($res == 0) {
4051
throw new \Exception('Error zipping files : ' . $this->oPclZip->errorInfo(true));
4152
}

src/Common/Adapter/Zip/ZipArchiveAdapter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ public function close()
3737
return $this;
3838
}
3939

40-
public function addFromString($localname, $contents)
40+
public function addFromString(string $localname, string $contents, bool $withCompression = true)
4141
{
4242
if ($this->oZipArchive->addFromString($localname, $contents) === false) {
4343
throw new \Exception('Error zipping files : ' . $localname);
4444
}
45+
if (!$withCompression) {
46+
$this->oZipArchive->setCompressionName($localname, \ZipArchive::CM_STORE);
47+
}
4548

4649
return $this;
4750
}

src/Common/Adapter/Zip/ZipInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ public function close();
3434
*
3535
* @throws \Exception
3636
*/
37-
public function addFromString($localname, $contents);
37+
public function addFromString(string $localname, string $contents, bool $withCompression = true);
3838
}

src/Common/Microsoft/PasswordEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class PasswordEncoder
114114
*
115115
* @return string
116116
*/
117-
public static function hashPassword(string $password, string $algorithmName = self::ALGORITHM_SHA_1, string $salt = null, int $spinCount = 10000)
117+
public static function hashPassword(string $password, string $algorithmName = self::ALGORITHM_SHA_1, ?string $salt = null, int $spinCount = 10000)
118118
{
119119
$origEncoding = mb_internal_encoding();
120120
mb_internal_encoding('UTF-8');

src/Common/XMLReader.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function getDomFromString(string $content)
108108
*
109109
* @return \DOMNodeList<\DOMElement>
110110
*/
111-
public function getElements(string $path, \DOMElement $contextNode = null)
111+
public function getElements(string $path, ?\DOMElement $contextNode = null)
112112
{
113113
if ($this->dom === null) {
114114
return new \DOMNodeList();
@@ -154,7 +154,7 @@ public function registerNamespace($prefix, $namespaceURI)
154154
*
155155
* @return \DOMElement|null
156156
*/
157-
public function getElement($path, \DOMElement $contextNode = null): ?\DOMElement
157+
public function getElement($path, ?\DOMElement $contextNode = null): ?\DOMElement
158158
{
159159
$elements = $this->getElements($path, $contextNode);
160160
if ($elements->length > 0) {
@@ -173,7 +173,7 @@ public function getElement($path, \DOMElement $contextNode = null): ?\DOMElement
173173
*
174174
* @return string|null
175175
*/
176-
public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null)
176+
public function getAttribute($attribute, ?\DOMElement $contextNode = null, ?string $path = null)
177177
{
178178
$return = null;
179179
if ($path !== null) {
@@ -200,7 +200,7 @@ public function getAttribute($attribute, \DOMElement $contextNode = null, $path
200200
*
201201
* @return string|null
202202
*/
203-
public function getValue($path, \DOMElement $contextNode = null)
203+
public function getValue($path, ?\DOMElement $contextNode = null)
204204
{
205205
$elements = $this->getElements($path, $contextNode);
206206
if ($elements->length > 0) {
@@ -218,7 +218,7 @@ public function getValue($path, \DOMElement $contextNode = null)
218218
*
219219
* @return int
220220
*/
221-
public function countElements($path, \DOMElement $contextNode = null)
221+
public function countElements($path, ?\DOMElement $contextNode = null)
222222
{
223223
$elements = $this->getElements($path, $contextNode);
224224

@@ -233,7 +233,7 @@ public function countElements($path, \DOMElement $contextNode = null)
233233
*
234234
* @return bool
235235
*/
236-
public function elementExists($path, \DOMElement $contextNode = null)
236+
public function elementExists($path, ?\DOMElement $contextNode = null)
237237
{
238238
return $this->getElements($path, $contextNode)->length > 0;
239239
}

src/Common/XMLWriter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class XMLWriter extends \XMLWriter
5353
* @param string $pTemporaryStorageDir Temporary storage folder
5454
* @param bool $compatibility
5555
*/
56-
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = null, $compatibility = false)
56+
public function __construct(int $pTemporaryStorage = self::STORAGE_MEMORY, ?string $pTemporaryStorageDir = null, bool $compatibility = false)
5757
{
5858
// Open temporary storage
5959
if ($pTemporaryStorage == self::STORAGE_MEMORY) {
@@ -121,7 +121,7 @@ public function getData()
121121
*
122122
* @return void
123123
*/
124-
public function writeElementBlock(string $element, $attributes, string $value = null)
124+
public function writeElementBlock(string $element, $attributes, ?string $value = null)
125125
{
126126
$this->startElement($element);
127127
if (!is_array($attributes)) {
@@ -143,7 +143,7 @@ public function writeElementBlock(string $element, $attributes, string $value =
143143
*
144144
* @return void
145145
*/
146-
public function writeElementIf(bool $condition, string $element, string $attribute = null, $value = null)
146+
public function writeElementIf(bool $condition, string $element, ?string $attribute = null, $value = null)
147147
{
148148
if ($condition) {
149149
if (is_null($attribute)) {

tests/Common/Tests/Adapter/Zip/AbstractZipAdapter.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ abstract class AbstractZipAdapter extends \PHPUnit\Framework\TestCase
1515
/**
1616
* Returns a new instance of the adapter to test
1717
*
18-
* @return \PhpOffice\Common\Adapter\Zip\ZipInterface
18+
* @return ZipInterface
1919
*/
2020
abstract protected function createAdapter(): ZipInterface;
2121

@@ -50,17 +50,43 @@ public function testClose(): void
5050
$this->assertSame($adapter, $adapter->close());
5151
}
5252

53-
public function testAddFromString(): void
53+
public function testAddFromStringWithCompression(): void
5454
{
55-
$expectedPath = 'file.test';
56-
$expectedContent = 'Content';
55+
$expectedPath = 'file.png';
56+
$expectedContent = file_get_contents(
57+
PHPOFFICE_COMMON_TESTS_BASE_DIR
58+
. DIRECTORY_SEPARATOR . 'resources'
59+
. DIRECTORY_SEPARATOR . 'images'
60+
. DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png'
61+
);
5762

5863
$adapter = $this->createAdapter();
5964
$adapter->open($this->zipTest);
60-
$this->assertSame($adapter, $adapter->addFromString($expectedPath, $expectedContent));
65+
$this->assertSame($adapter, $adapter->addFromString($expectedPath, $expectedContent, true));
6166
$adapter->close();
6267

6368
$this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath));
69+
$this->assertTrue(TestHelperZip::assertFileIsCompressed($this->zipTest, $expectedPath));
70+
$this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent));
71+
}
72+
73+
public function testAddFromStringWithNoCompression(): void
74+
{
75+
$expectedPath = 'file.png';
76+
$expectedContent = file_get_contents(
77+
PHPOFFICE_COMMON_TESTS_BASE_DIR
78+
. DIRECTORY_SEPARATOR . 'resources'
79+
. DIRECTORY_SEPARATOR . 'images'
80+
. DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png'
81+
);
82+
83+
$adapter = $this->createAdapter();
84+
$adapter->open($this->zipTest);
85+
$this->assertSame($adapter, $adapter->addFromString($expectedPath, $expectedContent, false));
86+
$adapter->close();
87+
88+
$this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath));
89+
$this->assertFalse(TestHelperZip::assertFileIsCompressed($this->zipTest, $expectedPath));
6490
$this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent));
6591
}
6692
}

0 commit comments

Comments
 (0)